N
N
novicheck2016-07-18 13:31:20
Python
novicheck, 2016-07-18 13:31:20

How to get field of Mongodb document?

There are many documents of the same type with the "number" field in the meringue on the server. The task is to get the sum of all "number". Tried to do like this:

nums = db.events.find({"eventName" : "Number Hard"})
num = 0
for numbers in nums:
    num = num + int((numbers, {"number": 1, "_id": 0}))

but it doesn't work. Can you suggest how to do it? It is advisable not to use Mongo commands in a loop because it takes a lot of time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2016-07-18
@novicheck

aggregate framework:
stackoverflow.com/questions/18969916/mongodb-sum-query
https://docs.mongodb.com/manual/reference/operator...

M
Maxim Vasiliev, 2016-07-18
@qmax

use map/reduce https://docs.mongodb.com/manual/core/map-reduce/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question