1
具有文件MongoDB的總結嵌套子文檔
[
{
"__v" : 21,
"_id" : ObjectId("546330dbb8926d177052e9ff"),
"code" : "WfvCc",
"description" : "",
"elements" : [
{
"_id" : ObjectId("546471f61e13b76a0b20ccaf"),
"comments" : [],
"meta" : {
"createdBy" : "545ab39ef1b0c88a695fcf8d",
"modifiedAt" : "1415868918045",
"createdAt" : "1415868918045"
},
"title" : "awesome title",
"votes" : {
"count" : 3,
"meta" : [
{
"createdBy" : "545ab39ef1b0c88a695fcf8d",
"_id" : ObjectId("546473831e13b76a0b20ccb7"),
"createdAt" : "1415869315618"
},
{
"createdBy" : "545aaddcf1b0c88a695fcf84",
"_id" : ObjectId("546473d71e13b76a0b20ccbc"),
"createdAt" : "1415869399584"
},
{
"createdBy" : "5461c0e2c9c39a192c44226c",
"_id" : ObjectId("546474041e13b76a0b20ccbe"),
"createdAt" : "1415869444056"
}
]
}
}
]
},
{
"__v" : 21,
"_id" : ObjectId("546330dbb8926d177052e9ff"),
"code" : "WfvCc",
"description" : "",
"elements" : [
{
"_id" : ObjectId("546471f61e13b76a0b20ccaf"),
"comments" : [],
"meta" : {
"createdBy" : "545ab39ef1b0c88a695fcf8d",
"modifiedAt" : "1415868918045",
"createdAt" : "1415868918045"
},
"title" : "awesome title",
"votes" : {
"count" : 3,
"meta" : [
{
"createdBy" : "545ab39ef1b0c88a695fcf8d",
"_id" : ObjectId("546473831e13b76a0b20ccb7"),
"createdAt" : "1415869315618"
},
{
"createdBy" : "545aaddcf1b0c88a695fcf84",
"_id" : ObjectId("546473d71e13b76a0b20ccbc"),
"createdAt" : "1415869399584"
},
{
"createdBy" : "5461c0e2c9c39a192c44226c",
"_id" : ObjectId("546474041e13b76a0b20ccbe"),
"createdAt" : "1415869444056"
}
]
}
}
]
}
]
名單我想又名聚集的用戶列表。橫跨文檔的elements.votes.meta.createdBy
並計算整個文檔中出現的總數。 *請注意,elements.votes.meta.createdBy
每個文檔都是唯一的,所以理論上這應該更簡單。
到目前爲止,我已經結束了與查詢:
db.sessions.aggregate(
{ $project: {
meta: "$elements.votes.meta"
}},
{ $unwind: "$meta" },
{ $group: {
_id: "voters",
voters: {
$addToSet: "$meta.createdBy"
}
}}
)
只是爲了得到完全被卡住一次。我知道我需要一個雙重分組,但似乎無法弄清楚。任何幫助讚賞。
真棒,稍加修改它正是我一直在尋找。關鍵是要加倍放棄這個集合。乾杯! – 2015-02-08 09:58:23