/* 1 */
{
"_id" : ObjectId("5941240cdc8dd12a183337b0"),
"item" : ObjectId("593e700d9f34350aac73231d"),
"store" : ObjectId("593e7589a489ce07c899210d"),
"itemcredits" : [
{
"expireDate" : "10/2019",
"unitePrice" : "50",
"itemCredit" : "5"
},
{
"expireDate" : "12/2018",
"unitePrice" : "50",
"itemCredit" : "3"
}
]
}
我想查询获取匹配相同商店和商品的同一文档的itemcredits.itemCredit的总和,以获得此示例的结果= 8
答案 0 :(得分:0)
尝试这样的事情
db.collectionName.aggregate([{$group: {
'_id': '$_id',
"total": {$sum: "$itemcredits.itemcredits" }
}}]);
itemcredits需要是整数