1
我有這樣的與文檔的集合:
MongoDB的聚合使用嵌套元素
"_id" : "15",
"name" : "empty",
"location" : "5th Ave",
"owner" : "machine",
"visitors" : [
{
"type" : "M",
"color" : "blue",
"owner" : "Steve Cooper"
},
{
"type" : "K",
"color" : "red",
"owner" : "Luis Martinez"
},
// A lot more of these
]
}
我想按visitors.owner找到其所有者擁有最參觀,我嘗試這樣做:
db.mycol.aggregate(
[
{$group: {
_id: {owner: "$visitors.owner"},
visits: {$addToSet: "$visits"},
count: {$sum: "comments"}
}},
{$sort: {count: -1}},
{$limit: 1}
]
)
但我總是得到數= 0,而不是對應一個所有者訪問:/
請幫
它的工作!我添加了「{$ sort:{visits:-1}}」來讓所有者訪問次數最多。非常感謝 – Fourat
@Fourat不用擔心:-) – chridam