1
在我的Mongo中擁有以下文檔我試圖獲取具有指定id的對象。這是我的Mongo文檔。 蒙戈版本:2.6在MongoDB中只檢索嵌套數組中的查詢對象
{
"_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"
}
]
}
]
}
]
}
我在尋找ID爲「高清」的對象,我想獲得這樣的結果:
{
"id" : "def",
"type" : "image",
"url" : "http://example.com"
}
下面我舉我試過,代碼實例搜索這個對象。
db.getCollection('myCollection').aggregate([
{"$match": {
"footers.rows.elements.id": "def"
}},
{"$group": {
"_id": "$footers.rows.elements"
}}
])
,其結果是:
{
"_id" : [
[
[
{
"id" : "def",
"type" : "image",
"url" : "http://example.com"
},
{
"id" : "ghi",
"type" : "image",
"url" : "http://example.com"
}
]
]
]
}
有什麼建議?
非常感謝。很有幫助。 –