我有以下文件:
{ "_id" : ObjectId("5d9db4462034bf17454d7d33"), "name" : "Product1", "cost_oneoff" : "1", "cost_monthly" : "1", "margin_oneoff" : "1", "margin_monthly" : "1", "price_oneoff" : "1", "price_monthly" : "1" }
{ "_id" : ObjectId("5d9dc2f2d8e17309b46f9b03"), "name" : "Product2", "cost_oneoff" : "0", "cost_monthly" : "1", "margin_oneoff" : "0,5", "margin_monthly" : "0,5", "price_oneoff" : "0", "price_monthly" : "2" }
我希望得到以下语句中每月费用的总和,例如:
。
{ "_id" : null, "total" : 0 }
有人能帮我吗?
db.service_items.aggregate([
{ $match: {$or: [{"_id": ObjectId("5d9db4462034bf17454d7d33")},{"_id": ObjectId("5d9dc2f2d8e17309b46f9b03")}]}},
{ $group:
{_id: null,
total: {
$sum: "$cost_monthly"
}
}
}
])
结果:
{ "_id" : null, "total" : 0 }
期望的答案是2。