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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -