我是mongoDB的新手。在给定的数据结构中,如何对misscallcount字段求和
{
"_id" : ObjectId("596c5f6905c36efd35000009"),
"misscallDetails" : [
{
"contactId" : "1573778945692669180",
"misscallCount" : NumberInt(1),
"promotedOn" : ISODate("2017-08-04T13:54:17.298+0000")
},
{
"contactId" : "1573778945692669180",
"misscallCount" : NumberInt(1),
"promotedOn" : ISODate("2017-08-04T13:56:17.243+0000")
}
],
"promoId" : ObjectId("596c5f4705c36efd35000003"),
}
来自评论:
我试着从我这边开始,但它给了总计0 ;
db.promoledger.aggregate( [
{ $group: { _id: null, total: { $sum: "$misscallDetails.misscallCount" } }}
])
答案 0 :(得分:0)
在为sum
分组之前,您需要$unwind
数组
b.promoledger.aggregate( [
{$unwind: '$misscallDetails'},
{ $group: { _id: null, total: { $sum: "$misscallDetails.misscallCount" } }}
])