pymongo - How would I sum the number of upvotes by specific field in mongodb -


i sum number of upvotes specific field in mongodb. example returning sum of number of upvotes author john has gotten.

{ "_id": "fc0996a9726e4bf08c3d614afe05c7a8", "blog_id": "f8da5ae532aa44f6a54d86fd822f7d52", "author": "john", "content": "hello", "upvotes": 3, } { "_id": "dea0a9713a734717920b652eed268b99", "blog_id": "f8da5ae532aa44f6a54d86fd822f7d52", "author": "john", "content": "hello", "upvotes": 2, } { "_id": "3d286c4ce54046f4aac9ea627a48f04e", "blog_id": "f8da5ae532aa44f6a54d86fd822f7d52", "author": "john", "content": "hello", "upvotes": 1, } { "_id": "6782b555dd8c47f58dba28e60fda4a5f", "blog_id": "f8da5ae532aa44f6a54d86fd822f7d52", "author": "123", "content": "bye", "upvotes": 2, } 

how sum upvotes , return value 6, of upvotes john has gotten. also, using python web app, flask.

thank you

you can use $group of aggregate framework:

db.collection.aggregate([{ $group: { _id: { author:'$author'}, totalupvotes: { $sum: "$upvotes" }}}]) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -