Here's my collection [test].
{"_id" : "Test1", "enabled": "on", "value" : 10},
{"_id" : "Test2", "enabled": "on", "value" : 50},
{"_id" : "Test3", "enabled": "on", "value" : 10},
{"_id" : "Test4", "value" : 5},
{"_id" : "Test5", "value" : 2}
I would like to get all the total of the value and total value of the field with "enabled":"on" like these:
Desired result:
[ { _id: null, totalValue: 77, totalEnabled: 70 } ]
Here's what i have so far but no luck.
db.collection('test').aggregate({
$group: {
_id: null,
totalValue : {
$sum: "$value"
},
totalEnabled: $sum : {"enabled":{$exists:true}}
}
}, function(err, result) {
if (err) return console.dir(err)
console.log(result);
});