How to query a nested document in mongo?

Retrieving an embedded document from the a mongo document?

  • i had a mongo document structured as {    "id": 1,    "userId": 1,    "layout": 1,    "snapshotDetails"â–¼: {      "0": {        "id": 1,        "name": "jaison",        "type": "justus",        "width": 100,        "height": 100,        "position": 1     },      "1": {        "id": 2,        "name": "jatin",        "type": "justus",        "width": 100,        "height": 100,        "position": 2     }   },    "_id": ObjectId("4f58932309ac38f808000002") } and i need to extract the particular embedded document "1": {        "id": 2,        "name": "jatin",        "type": "justus",        "width": 100,        "height": 100,        "position": 2     }  under snapshotDetails. for that i build a query like db.MasterDashboard.find({ "userId" : 1, "http://snapshotDetails.id" : 1 }, { "snapshotDetails" : 1 }); But i am not getting the output properly. the output of the query is [ "0": {        "id": 1,        "name": "jaison",        "type": "justus",        "width": 100,        "height": 100,        "position": 1     },      "1": {        "id": 2,        "name": "jatin",        "type": "justus",        "width": 100,        "height": 100,        "position": 2     } ]   is any issue in my query. please help

  • Answer:

    By default, MongoDB queries are designed to query entire http://documents.So when you run a find on this: { "userId" : 1, "http://snapshotDetails.id" : 1 } You are actually saying this: "find all documents with userId equal to 1 and having a snapshotDetail Id equal to 1" Now you also request a special return value: { "snapshotDetails" : 1 } And that's what you are getting back. You are getting the whole snapshotDetails field. What you actually want is the just the snapshotDetails that matched. Unfortunately, there is no way to do this. You can look at the docs here, you will see that feature simply does not exist. http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields You are not completely out of luck. If you take a look at the new Aggregation Framework, this has the features you need. http://www.mongodb.org/display/DOCS/Aggregation+Framework You should be able to do this with project and match.

Gaëtan Voyer-Perrault at Quora Visit the source

Was this solution helpful to you?

Other answers

If you use a ODM framework like Mongoose, you can do the query that Gaetan is suggesting and then use a helper function that the Mongoose framework provides with its DocumentArrays to retrieve a particular embedded document by id.

Thomas Chen

db.collectionname.aggregate([    // Match the documents by query    { "$match": {        "id" : "1",          "snapshotDetails.1."id": { "$in": [ 2 ] },    }},    // De-normalize nested array    { "$unwind": "$snapshotDetails" },    { "$unwind": "$snapshotDetails.1" },    // Filter the actual array elements as desired    { "$match": {          snapshotDetails.1."id": { "$in": [ 2 ] },    }},])

Shibi M

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.