In mongodb, the sum of $sum decimals is an integer, and all the numbers after the decimal point are forcibly removed.

the data in the database is like this. There are decimals, and I need to sum them.

execute the following code:

bet.aggregate(
  {
    $match: {
      state: { $in: [0, 1, 2, 3] }
    }
  },
  {
    $group: {
      _id: { state: "$state" },
      serviceCharge: { $sum:  "$serviceCharge" },
      percentage: { $sum: "$percentage" },
      income:{ $sum: { $subtract: [ "$serviceCharge","$percentage"] } }
    },
  },
  { $project: { "_id": 0, "state": "$_id.state", "serviceCharge": 1, "percentage": 1 ,"income":1} },
  (err, ret) => {
    console.log(ret)
  })
The result of

is:
[
{serviceCharge: 6, percentage: 0, income: 6, state: 2},
{serviceCharge: 57, percentage: 38, income: 19, state: 0}
]
Why is the result an integer? Solve!

Mar.04,2021
Menu