【发布时间】:2017-11-10 17:43:57
【问题描述】:
我有以下 MongoDB 查询:
db.foo.aggregate([
{$unwind: "$Calculation.CustSatisfaction"} // important step!
,{$group: {_id: {country: "$Country",
company: "$Company_ID",
staff: "$Staff_ID",
year: "$Calculation.CustSatisfaction.Trans_Year",
mon: "$Calculation.CustSatisfaction.Trans_Month"},
tHH: {$sum: "$Calculation.CustSatisfaction.HH"},
tHN: {$sum: "$Calculation.CustSatisfaction.HN"}
}}
]);
但是,如果有多个月份,它将给我多行。如何更改我的查询以使每个员工只有如下一行:
{
"_id" : {
"country" : "MY",
"company" : "MY01",
"staff" : "NBJ64"
},
"Cust": [ {
"year": 2017,
"month": 9
"tHH" : 8,
"tHN" : 0
},
{
"year": 2017,
"month": 8
"tHH" : 7,
"tHN" : 0,
}
},
{
"_id" : {
"country" : "MY",
"company" : "MY01",
"staff" : "NBJ50"
},
"Cust": [ {
"year": 2017,
"month": 9
"tHH" : 1,
"tHN" : 0
},
{
"year": 2017,
"month": 8
"tHH" : 4,
"tHN" : 0,
}
}
我不知道该怎么做。感谢任何帮助。
【问题讨论】:
标签: mongodb aggregation-framework