Rounding the outcome of a formula on a field?

So how could i round the outcome of a sum and divide? Because i need a specific number but this number would never be the same.

I tried this:

 $set: {

    power: { $sum: ["$power", "$attack", "$defence", { $divide: ["$endurance", 3] },  { $round: [$sum, 2] }] }

 }

And this:

 $set: {

    power: { $sum: ["$power", "$attack", "$defence", { $divide: ["$endurance", 3] },  { $round: [power, 2] }] }

 }

But both turn up:
ReferenceError: power is not defined
ReferenceError: $sum is not defined

Right now the output is: 9.333333333333334, but i would like 9.3.

Try with

{ $round: [ { $sum: ["$power", "$attack", "$defence", { $divide: ["$endurance", 3] } , 2] }

that matches

Won’t work: MongoError: Unrecognized pipeline stage name: ‘$round’

From your other thread, I deduce that power, attack and defence are integers so I would try to $round only the last term of the sum with:

{ $round : [ { $divide : [ $endurance , 3 ] } , 2 ] }
1 Like