Getting specific inline document from MongoDB query

I have a MongoDB 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") 
}

      

I need to extract a specific inline document "1" in "snapshotDetails" like this:

"1": {"id": 2, "name": "jatin", "type": "justus", "width": 100, "height": 100, "position": 2}

To do this, I build a query like this:

db.MasterDashboard.find({
"userId" : 1,
"snapshotDetails.id" : 1
},
{
"snapshotDetails" : 1
});

      

But I am not getting the output correctly. Query result

[
"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 
    }
] 

      

Can anyone please identify the issue with this query and suggest how to simply return the inline document I want?

+3


source to share


2 answers


I think your problem is that it is returning the full content of the "snapshotDetails" subdocument, which is what the ask asks you are asking about. Try changing the projection you are using. I pasted in your sample document from the top and then ran:

db.foo.find({"userId" : 1, "snapshotDetails.0.id" : 1}, 
            { "_id" : 0, "snapshotDetails.1" : 1})

      



Then it should only return sub-document "1", this is what I got as output:

{ "snapshotDetails" : { "1" : 
                      { "id" : 2, 
                        "name" : "jatin", 
                        "type" : "justus", 
                        "width" : 100, 
                        "height" : 100, 
                         "position" : 2 
}}}

      

+1


source


This is a late answer, but I think it will be helpful to others. Suppose snapshotDetails can be rewritten as a valid array, e.g .:

{
    "_id" : ObjectId("4f58932309ac38f808000002"),
    "id" : 1,
    "userId" : 1,
    "layout" : 1,
    "snapshotDetails" : [ 
        {
            "id" : 1,
            "name" : "jaison",
            "type" : "justus",
            "width" : 100,
            "height" : 100,
            "position" : 1
        }, 
        {
            "id" : 2,
            "name" : "jatin",
            "type" : "justus",
            "width" : 100,
            "height" : 100,
            "position" : 2
        }
    ]
}

      

aggregate operations can be applied to slicing, filtering, and reformatting the document in any way. As an example, considering doc as a collection of containers, the following command will produce the desired output.

db.doc.aggregate([{$unwind:'$snapshotDetails'},{$project:{details:'$snapshotDetails' , _id:0}}, {$match:{'details.id':2}}])

      



Result:

{
    "result" : [ 
        {
            "details" : {
                "id" : 2,
                "name" : "jatin",
                "type" : "justus",
                "width" : 100,
                "height" : 100,
                "position" : 2
            }
        }
    ],
    "ok" : 1
}

      

More info at MongogDB Aggregations

+1


source







All Articles