php - How to get a specific object from array of objects inside specific MongoDB document? -
there struct of mongodb documents:
{ _id : "12345", name : "foo", object : { array_of_objects : [{ id : 507, name : "some name", dt : "2012-06-27 16:35:50" },{ id : 506 name : "some other name", dt : "2012-06-21 16:09:05" }, … ] } }
i need object array_of_objects id of document specified name. use php , tried execute next code:
$collection->find(array('name' => 'foo', 'object.array_of_objects.id' => 507));
it returns elements of array_of_objects instead of element id 507. after try make query $elemmatch:
$collection->find(array('name' => 'foo', 'object.array_of_objects' => array('$elemmatch' => array('id' => 507))));
but had return same. :( mongodb version 2.0.6. please help.
this issue has been resolved , available in mongodb version 2.2, next stable release: https://jira.mongodb.org/browse/server-828
i tried using mongodb 2.1.2 (unstable, development release):
sample doc:
{ "_id" : 1, "object" : { "array" : [ { "id" : 507, "name" : "jenna" }, { "id" : 506, "name" : "matt" } ] } }
query:
db.food.find({_id:1, "object.array.id":506},{_id:0, "object.array.$":1})
result:
{ "object" : { "array" : [ { "id" : 506, "name" : "matt" } ] } }
Comments
Post a Comment