Having the following document in my Mongo I'm trying to get the object with specified id. Here is my Mongo document. Mongo version: 2.6
{
"_id" : ObjectId("57c1ae9ac1bd31d4eb4d546d"),
"footers" : [
{
"type" : "web",
"rows" : [
{
"id" : "abc",
"elements" : [
{
"id" : "def",
"type" : "image",
"url" : "http://example.com"
},
{
"id" : "ghi",
"type" : "image",
"url" : "http://example.com"
}
]
}
]
}
]
}
I'm looking for an object with id "def", and I want to obtain this result:
{
"id" : "def",
"type" : "image",
"url" : "http://example.com"
}
Below I cite the example of code that I tried to make search of this object.
db.getCollection('myCollection').aggregate([
{"$match": {
"footers.rows.elements.id": "def"
}},
{"$group": {
"_id": "$footers.rows.elements"
}}
])
And the result is:
{
"_id" : [
[
[
{
"id" : "def",
"type" : "image",
"url" : "http://example.com"
},
{
"id" : "ghi",
"type" : "image",
"url" : "http://example.com"
}
]
]
]
}
Any suggestions?