Friday, 6 September 2013

Understanding the "_id" key in JSON results accessed from a PHP file.

Understanding the "_id" key in JSON results accessed from a PHP file.

PHP
$query = $myCollection->findOne(array("field2.sf2" => "value two"));
echo json_encode($query);
Returned JSON Object
{"_id":{"$id":"5476854783473474578548"},"field1":"value
one","field2":{"sf1":["av1","av2","av3"],"sf2":"value
two"},"field3":"value three"}
What is happening at:
"_id":{"$id":"5476854783473474578548"}
I can see it is the representation of the Document's "_id" key and value ie:
"_id": ObjectId("5476854783473474578548")
But a few things are happening:
The value becomes a sub document ie it is surrounded in curly braces
ObjectId is being replaced by "$id"
I'm using MongoDB, accessed by a PHP file, via jQuery's getJSON() method.
Are there any gotcha's i need to look out for with this happening?
Any commonly known 'industry knowledge' tips that could be helpful to a
MongoDB newbie or further explain what is happening?

No comments:

Post a Comment