pymongo - Is it possible to sum 2 fields in MongoDB using the Aggregation framework? -
i have collection documents contain fields type, totala , totalb
i want use aggregation framework in order group type - , sum of both totala , totalb together.
the last thing tried (doesn't work) is:
'$group' : { '_id' : '$type', 'totala' : { '$sum' : '$totala' }, 'totalb' : { '$sum' : '$totalb' }, 'totalsum' : { '$sum' : '$totala', '$sum' : '$totalb' }, } }
totalsum has sum of 1 of fields instead of combined value.
i found solution:
just using $project $add 2 fields in output.
{ "$project" : { 'totala' : '$totala', 'totalb' : '$totalb', 'totalsum' : { '$add' : [ '$totala', '$totalb' ] }, }
Comments
Post a Comment