Reputation: 2793
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 the specific "1" embedded document under "snapshotDetails" like so:
"1": { "id": 2, "name": "jatin", "type": "justus", "width": 100, "height": 100, "position": 2 }
To do that I build a query something like this:
db.MasterDashboard.find({
"userId" : 1,
"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
}
]
Can anyone spot the problem with this query and suggest how to just return the embedded document I want?
Upvotes: 3
Views: 2973
Reputation: 193
This is a late answer but I think it will be useful for others. Supposing that snapshotDetails can be rewritten to be a proper array like:
{
"_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 slice, filter and reformat the document in any way needed. As an example, considering doc as the container collection, the following command would produce the desired result.
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 information at MongoDB Aggregations
Upvotes: 2
Reputation: 21692
I think your issue is that it is returning the full content of the "snapshotDetails" sub-document, which is what the projection you are specifying is asking for. Try changing the projection you are using. I inserted your sample doc from above and then ran:
db.foo.find({"userId" : 1, "snapshotDetails.0.id" : 1},
{ "_id" : 0, "snapshotDetails.1" : 1})
Then it should only return the "1" sub-document, here's what I got as the output:
{ "snapshotDetails" : { "1" :
{ "id" : 2,
"name" : "jatin",
"type" : "justus",
"width" : 100,
"height" : 100,
"position" : 2
}}}
Upvotes: 1