Skip to content

Access document 'id' in query #24

russellseymour opened this Issue Jan 12, 2012 · 3 comments

3 participants


Good evening,

I have started implementing Morph into my project and things are going really well - thanks for the great library.

However I have got to a stumbling block. How do you get the 'id' of the documents in the returned query?

If I print out a raw output of the returned document I can see that there is a protected $id with the MongoId. However as the documents returned are based on my own class I cannot get access to it using the construct:

$doc -> {'$id'}

Please can you advise as to how I can get the ID of the document?

Thanks very much,

Russell Seymour

kkamkou commented Jan 12, 2012

Hi there, did you tried to use $doc->id()?


Cheers @kkamkou - You are spot on.

Because the id field in mongodb is 'special' it is not accessible as a public property like the other fields. It is simply wrapped in an function.


Brilliant thank you, that worked.

After I had posted the message I looked at the code whilst on the train on the way home and saw that this was the way to get the ID. Thanks for confirming it for me.

Regards, Russell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.