Javascript 计算mongodb中对象的总值
这是我查找总值的查询。我使用$projectJavascript 计算mongodb中对象的总值,javascript,mongodb,object,Javascript,Mongodb,Object,这是我查找总值的查询。我使用$project { "id" : "12345" "energy" : "electricity", "type" : "lighting", "value" : 24324 }, { "id" : "12345" "energy" : "electricity", "type" : "equipment", "value
{
"id" : "12345"
"energy" : "electricity",
"type" : "lighting",
"value" : 24324
},
{
"id" : "12345"
"energy" : "electricity",
"type" : "equipment",
"value" : 24324
},
{
"energy" : "electricity",
"type" : "fans",
"value" : 24324
},
{
"id" : "12345"
"energy" : "electricity",
"type" : "distribution",
"value" : 24324
},
{
"id" : "12345"
"energy" : "electricity",
"type" : "chiller",
"value" : 24324
},
{
"id" : "12345"
"energy" : "electricity",
"type" : "other",
"value" : 24324
}
我试图找到总价值。请任何人帮忙找出总价值。还可以使用Id-wise查找值。db.products.aggregate([{$group:{{{{u Id:'$type',total:{$sum:'$value'}}}}}])谢谢@Joel Garcia Nuño,但是我如何找到Id:“12345”尝试在{u Id:'中过滤的总数,它将等于12345 Id,但其中一个没有,您可能需要检查它是否为null,不要做任何事情
db.invoices.aggregate([
{
$project: {
total: {$sum : "$value"}
}
}])