SpringerJerry
SpringerJerry

Reputation: 308

Mongodb how to round a calculated value

So i tried using $round on a calculated value but i still cant get it to work. The current value now is: 9.333333333333334, but i would like to have: 9.3.

This is what i tried:

{
     $set: {
        power: { $sum: ["$power", "$attack", "$defence", { $divide: ["$endurance", 3] },  { $round: ["$power", -1] }] }
     }
  }
];

But this won't round the output, how do i fix this?

Upvotes: 0

Views: 227

Answers (1)

Haniel Baez
Haniel Baez

Reputation: 1690

$round has this syntax:

{ $round : [ <number>, <place> ] }
  • <number> can be any valid expression that resolves to a number.
  • <place> is optional and can be any valid expression that resolves to an integer between -20 and 100.

enter image description here

You code should look like:

{ $round : [ "$power", 1 ] }

Upvotes: 1

Related Questions