You can use the $round operator in MongoDB to round numeric values to a certain number of decimal places.
This operator uses the following basic syntax:
db.myCollection.aggregate([ {$project:{rounded_value: { $round: [ "$value", 1 ] }}} ])
This particular example rounds the values in the field “value” to one decimal place.
The following examples show how to use this syntax with a collection teams with the following documents:
db.teams.insertOne({team: "Mavs", points: 31.345}) db.teams.insertOne({team: "Spurs", points: 22.88}) db.teams.insertOne({team: "Rockets", points: 19.91}) db.teams.insertOne({team: "Warriors", points: 26.5}) db.teams.insertOne({team: "Cavs", points: 33})
Example 1: Round Values to One Decimal Place
We can use the following code to round the values in the “points” field to one decimal place:
db.teams.aggregate([ {$project:{rounded_points: { $round: [ "$points", 1 ] }}} ])
Here is the output:
{ _id: ObjectId("6203d3b11e95a9885e1e763b"), rounded_points: 31.3 } { _id: ObjectId("6203d3b11e95a9885e1e763c"), rounded_points: 22.9 } { _id: ObjectId("6203d3b11e95a9885e1e763d"), rounded_points: 19.9 } { _id: ObjectId("6203d3b11e95a9885e1e763e"), rounded_points: 26.5 } { _id: ObjectId("6203d3b11e95a9885e1e763f"), rounded_points: 33 }
Notice that each value in the “points” field has been rounded to one decimal place.
Example 2: Round Values to Zero Decimal Places
Note that if you don’t specify a number of decimal places, MongoDB will automatically round to zero decimal places.
For example, suppose we use the $round operator with the following syntax:
db.teams.aggregate([ {$project:{rounded_points: { $round: [ "$points"] }}} ])
Here is the output:
{ _id: ObjectId("6203d3b11e95a9885e1e763b"), rounded_points: 31 } { _id: ObjectId("6203d3b11e95a9885e1e763c"), rounded_points: 23 } { _id: ObjectId("6203d3b11e95a9885e1e763d"), rounded_points: 20 } { _id: ObjectId("6203d3b11e95a9885e1e763e"), rounded_points: 26 } { _id: ObjectId("6203d3b11e95a9885e1e763f"), rounded_points: 33 }
Notice that each points value is rounded to zero decimal places.
Example 3: Round Values & Display Other Fields in Output
We can use a value of 0 (don’t display) or 1 (display) to also show other fields in the output.
For example, the following code shows how to round the points values to two decimal places and also display the “team” field and the “points” field in the output:
db.teams.aggregate( [ { $project: { _id: 0, team: 1, points: 1, rounded_points: { $round: [ "$points", 2 ] } } } ] )
Here is the output:
{ team: 'Mavs', points: 31.345, rounded_points: 31.34 } { team: 'Spurs', points: 22.88, rounded_points: 22.88 } { team: 'Rockets', points: 19.91, rounded_points: 19.91 } { team: 'Warriors', points: 26.5, rounded_points: 26.5 } { team: 'Cavs', points: 33, rounded_points: 33 }
Note: You can find the complete documentation for the $round function here.
Additional Resources
The following tutorials explain how to perform other common operations in MongoDB:
MongoDB: How to Add a New Field
MongoDB: How to Remove a Field
MongoDB: How to Count Distinct Values in Field