I have a MongoDB collection to store users balance and score. I plan to use aggregate function, where with a userID, the aggregation will get all documents with the userID, then sum up the 'balance' and 'score' field inside each documents.Here is what I have done:UserHistory.aggregate([ { $match: { userID: userID }}, { $group: { balance: { $sum : "$balance"}, score: { $sum : "$score"}, } } ]) The problem is, I also plan to count the number of documents of the user. Once the aggregation has filtered off and found all the documents matching the userID, I hope to count the number of documents before it gets grouped in the next step. Essentially, it's between the $match and the $group steps.The final result should be an object with 'balance', 'score', and also 'count' that indicates the number of that particular user's documents.
Submitted February 07, 2020 at 05:27PM by VickNicks
No comments:
Post a Comment