3

There is struct of my 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 to get an object from array_of_objects with a certain id of the document with the specified name. I use php and tried to execute the next code:

$collection->find(array('name' => 'foo', 'object.array_of_objects.id' => 507));

It returns all elements of array_of_objects instead of element with id 507. After that I try to make query with $elemMatch:

$collection->find(array('name' => 'foo', 'object.array_of_objects' => array('$elemMatch' => array('id' => 507))));

But it had return the same. :( My MongoDB version 2.0.6. Please help.

2
  • Drawback of MongoDB: stackoverflow.com/questions/10042097/… Commented Jun 28, 2012 at 18:10
  • @david-cheung thanks! So, I'll try to do this with Mapreduce. Commented Jun 28, 2012 at 19:15

3 Answers 3

3

This issue has been resolved and will be available in MongoDB version 2.2, the next stable release: https://jira.mongodb.org/browse/SERVER-828

I just tried it 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" } ] } }
Sign up to request clarification or add additional context in comments.

1 Comment

Good news everyone! 8) Thanks, @Jenna!
0

You cannot do this with MongoDB: MongoDB won't drill down into a particular document to extract an item from an array for instance, you have to do this by yourself.

Now the PHP driver might do more work, I don't know -- I'm speaking about MongoDB core queries.

3 Comments

You can return a subset of fields- check out the MongoDB documentation: mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields
@Jenna yes you're right -- but a specific item from an array based on its value ?
As I understand, it can be done either by getting the entire array of objects and the search for the desired item using PHP, or by using MapReduce, right?
0

So, I did it with MapReduce. Hopefully, this will save someone time and nerves.

$name = 'foo';
$id = 507;

$mongo = new Mongo('…');

$db = $mongo->my_db;

$map = new MongoCode('function(){'.
    'this.object.array_of_objects.forEach(function(obj){'.
        'if (obj.id == '.$id.')'.
            'emit(1, obj);'.
    '});}'
);

$reduce = new MongoCode('function(k,v){ return [k,v]; }');

$response = $db->command(array(
    'mapreduce' => 'my_collection', 
    'map' => $map,
    'reduce' => $reduce,
    'query' => array('name' => $name),
    'out' => array('inline' => 1)
));

var_dump($response['result']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.