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:
sub document
ie it is surrounded in curly bracesObjectId
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?
The objectId Is not being replaced but merely serialised to string form when you call json_encode
.
Its properties are being taken out, placed inside stringified JSON and sent over the wire where then in JQuery the library parses that stringified JSON into the object you see.
No data has been lost.
The only gotcha I can think of is that the constructor for MongoId
does not actually take this object back in, it assumes only a hexadecimal ObjectId (i.e. 5476854783473474578548
) http://www.php.net/manual/en/mongoid.construct.php