Is it possible to sum 2 fields in MongoDB using the Aggregation framework?
I found a solution:
Just using $project to $add the two fields together in the output.
{ "$project" : {
'totalA' : '$totalA',
'totalB' : '$totalB',
'totalSum' : { '$add' : [ '$totalA', '$totalB' ] },
}
You can use $sum
in this way:
{
$group : {
_id: null,
amount: { $sum: { $add : [
'$NumberOfItemsShipped', '$NumberOfItemsUnshipped'
]}},
}
},