Answer the question
In order to leave comments, you need to log in
How to calculate sum of MongoDB fields?
Hello! The question is. How to calculate the sum of fields for each user in MongoDB?
I can't figure out how to form the correct request.
uid = user id, count - column of fields to be counted for all users with a certain uid for a certain period of time.
today = datetime.datetime.now()
count_day = db.logs.find({'uid': a['uid']}, {'date': {"$gt": today - datetime.timedelta(1)}}).count()
Answer the question
In order to leave comments, you need to log in
db.logs.aggregate([
{
$match: { uid: a['uid'] }
},
{
$group: {
_id: '$uid',
sum: { $sum: '$count' }
}
}
])
$match: { uid: a['uid'], date: {"$gt": today - datetime.timedelta(1)} }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question