Total number of one parameter in all documents

Hello, I need help, I want to get the TOTAL number of one parameter in the document. How can this be done quickly, I tried to do it through “reduce”, tried through $group $sum, it does not work, or the answer is “undefined”, or [ object Promise ], I have been suffering for 5 hours, I can not do such a simple thing.

Briefly what I want:
User = is collection
I want to sum the “money” field in all documents in the collection.

Hi @Ya_Strelok,

Welcome to MongoDB community!

I believe a $group of null will allow you to do that:

db.user.aggregate([{$group : { _id : null, total : {$sum : "$money"}}}]) 

https://docs.mongodb.com/manual/reference/operator/aggregation/group/#group-by-null

Best
Pavel

1 Like