0

I have a nested document of following format:

{
     _id: "1234567890",
     course: {content: [{   id: 1,
                            children:[{
                             id: 1111
                             },
                             {
                             id: 2222
                             }
                              {
                             id: 3333
                             }]
                        },
                        {   id: 2,
                            children:[{
                             id: 4444
                             },
                             {
                             id: 5555
                             }
                              {
                             id: 6666
                             }]
                        }

              ]}

}

I'm trying to query the children based on id (course.content.children.id) using the following code from Node.js:

var query = {
            'course.content.children.id': quizId,
};
var option = {
        'course.content.children.$': 1,
        };
db.getEntityWithOption(tablename,query,option,function(data,err){
    res.send(data.course.content[0].children);
});

db.getEntityWithOption is wrapper method on top of db.find() method. when I call the method with quiz id 1, the above code return me the following complete array: `

                            {   id: 1,
                            children:[{
                             id: 1111
                             },
                             {
                             id: 2222
                             }
                              {
                             id: 3333
                             }]
                        }

But I need only the specific element of the array with id 1111 not the compete array. Can any life savior suggest me what I'm doing wrong here. TIA :)

  • possible duplidate of http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – Ben Feb 26 '16 at 14:08
  • @Ben I already gone through the link, it not working for me, its returning me complete Array set as mentioned. – Ashish Anand Feb 26 '16 at 14:24

3 Answers3

0

Well you can find the id what you want from the children object then send the result like:

res.send(data.course.content[0].children[0]);

Or you can find it with a for loop if its not in the first array element then sent the result.

Or you can try using $elemMatch in your query but then you will have to use aggregation.

Btw in my opinion you should not use so deeply nested documents cuz it will be hard to run complex queries on.

kailniris
  • 8,856
  • 2
  • 17
  • 24
0

Please try it through aggregation, with $unwind and $match

> db.course.aggregate([
                       // unwind the `content` array
                       {$unwind: '$course.content'}, 
                       // unwind the `children` array of `content`
                       {$unwind: '$course.content.children'}, 
                       // match the id with `1111`
                       {$match: {'course.content.children.id': 1111}}]);

Result:

{ "_id" : ObjectId("56d058d22750efa1f9dc3772"), "course" : { "content" : { "id" : 1, "children" : { "id" : 1111 } } } }

To meet you requirement, only the the 1111 array is returned, we can do that through $project

> db.course.aggregate([{$unwind: '$course.content'}, 
                       {$unwind: '$course.content.children'},
                       {$match: {'course.content.children.id': 111}}, 
                       {$project: {_id: 0, arr: '$course.content.children'}}]);

Result:

{ "arr" : { "id" : 111 } }
zangw
  • 43,869
  • 19
  • 177
  • 214
-1

You want to query by a property but you do not know which child of the content and children arrays possess the requested id. MongoDB provides the positional operator

The query should be defined this way:

var query = {
  'course.content.$.children.$.id': quizId,
};
gnerkus
  • 11,357
  • 6
  • 47
  • 71