I have a collection with these example documents:{name: "james", balance: "7.10", dateCreated: ISODate(...)} {name: "peter", balance: "5.00, dateCreated: ISODate(...)} {name: "michael", balance: "9.50", dateCreated: ISODate(...)} {name: "james", balance: "5.00", dateCreated: ISODate(...)} How can I sort them based on the sum of their 'balance'? In the example above, the top ranking should be 'james', because the sum of its 'balance' is the highest which is 7.10 + 5.00 = 12.10. Second will be 'michael' and third 'peter'.Basically, I would like to sum the documents based on 'balance' field, then get the top 10 documents based on their summed 'balance'. I would also like to retrieve the result from midnight of that day. For example, if this query is done on Monday 4PM, then the result retrieved should be from Monday 12AM until 4PM on the same day.I tried the following: coll.aggregate([ {$group: {_id: "$name"}, totalBalance: {$sum: "$balance"}}, {$sort: {totalBalance: -1}}, {$limit: 10} ]).exec(function (err, result) {...}) But I got Arguments must be aggregate pipeline operators error. Not sure where went wrong.
Submitted March 03, 2020 at 03:32PM by VickNicks
No comments:
Post a Comment