Add all field (Amount)

I’m trying to sum up all amount field in my database with aggregate and sum like this : TransferModel.aggregate([{ $group: { _id: null, "TotalAmount": { $sum: "$amount" } } }])
But am getting 0 as the response like this : { "_id": null, "TotalAmount": 0 }
Please is there anything am doing wrong? I would really appreciate if anybody can help, Thanks

Hello @Emmanuel_Oluwatimilehin,

Your query looks good, probably there are some minor issues, try to make sure the below things,

  • the amount property should have a numeric type value
  • confirm whether the collection has documents or not by await TransferModel.countDocuments({})
1 Like

In addition to

make sure that your property is really amount?

It could be Amount since you seem to favour an upper case as the first letter since your collection is TransferModel and your result field is TotalAmount.

Sharing some sample documents would be helpful.

2 Likes