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

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

java - Using an Integer ArrayList in Android -