MongoDB $round Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $round?

$round is a MongoDB aggregation expression that rounds a number to a whole integer or to a chosen decimal place. When the deciding digit is exactly 5, MongoDB uses round-half-to-even (banker’s rounding).

Remember
$round = round to nearest (half → even)

{ $round: [ <number>, <place> ] }

place omitted / 0 → whole number
place > 0        → that many decimals
place < 0        → tens, hundreds, …

1234.5678, 2  → 1234.57
1234.5678, 0  → 1235
1234.5678, -2 → 1200
UsePrefer
Nearest value$round
Always up / down$ceil / $floor
Chop decimals (no round)$trunc

How to Use $round in MongoDB

Pass a number and an optional place value.

  1. Choose an expression stage ($addFields or $project).
  2. Pass [ "$field", place ] (or omit place to round to an integer).
  3. Use positive place for money-style decimals; negative for tens/hundreds.
  4. Remember half-to-even when values land exactly on .5.
PieceRequired?Role
NumberYesint, long, double, or decimal expression
PlaceNoInteger; default 0; range -20 < place < 100
Result type—Matches the input numeric type

Pseudocode

Pseudocode
for each document:
    n = evaluate(numberExpression)
    p = evaluate(place) or 0
    if n is null or missing:
        result = null
    else if n is NaN / ±Infinity:
        result = n
    else:
        result = round_half_to_even(n, p)
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while rounding in pipelines.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $round: [ number, place? ] }
Default place0 (integer)
Half ruleRound to nearest even
Null / missingnull
Non-numericPipeline error
Currency tipPrefer integer cents over floats when possible

Live Preview

Pick a number and a place value. This preview uses the same half-to-even rule as MongoDB $round.

Simulates $round with round-half-to-even (same idea as the mongosh examples).

Result
n = 12.346, place = 2
$round → 12.35

{ rounded: 12.35 }

Worked Walkthrough — Place Values

Same number, three place settings—plus one half-to-even check.

Calculation
n = 1234.5678

$round([n, 2])  → 1234.57   // two decimals
$round([n, 0])  → 1235      // whole number
$round([n, -2]) → 1200      // nearest hundred

Half-to-even (place 0):
  10.5 → 10   (10 is even)
  11.5 → 12   (12 is even)
  2.5  → 2    (2 is even)

Tip: JavaScript Math.round(2.5) → 3; MongoDB $round → 2.

MongoDB Examples

We use a small products collection with a price field. First we round to cents (2 decimals), then to a whole number. Insert the sample data once in mongosh.

Example 1 — Sample Data

Imagine three products with decimal prices. We save them in a collection named products.

mongosh
db.products.insertMany([
  { _id: 1, name: "Mug", price: 12.346 },
  { _id: 2, name: "Pen", price: 2.5 },
  { _id: 3, name: "Book", price: 19.99 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in products. Run find() to see them:

mongosh
db.products.find()

Read those documents like this:

Field in MongoDBExamplePlain English
_id1Product id for this lesson
name"Mug"Product name
price12.346Number we will round
Picture
Mug   12.346  → round @2 → 12.35
Pen    2.5    → round @0 → 2     (half-to-even)
Book  19.99   → round @0 → 20

How It Works

  1. Insert once. insertMany adds three product documents.
  2. Prices are numbers. Not strings—$round needs a numeric type.
  3. find() shows the same shape. What you inserted is what you get back—ready for $round next.

Example 2 — Round to 2 Decimals

Goal: add price_2dp rounded to cents (two decimal places).

mongosh
db.products.aggregate([
  {
    $addFields: {
      price_2dp: { $round: [ "$price", 2 ] }
    }
  }
]);

How It Works

  1. Read the stored price. "$price" is the number for each product.
  2. Place = 2. Look two digits after the decimal, then round.
  3. Mug. 12.346 → third decimal is 6 (≥ 5 and not a pure half) → 12.35.
  4. Already fine. Pen and Book already fit in two decimals, so they stay the same.

Example 3 — Round to Integer

Same products, but round to a whole number (place: 0). Watch Pen: 2.5 becomes 2 because of half-to-even.

mongosh
db.products.aggregate([
  {
    $addFields: {
      price_int: { $round: [ "$price", 0 ] }
    }
  }
]);

How It Works

  1. Same stored documents. Still using the three products from Example 1.
  2. Place = 0. Round using the first digit after the decimal.
  3. Normal cases. Mug 12.346 → 12; Book 19.99 → 20.
  4. Half-to-even. Pen 2.5 is exactly halfway; nearest even integer is 2, not 3.

Edge Cases & Pitfalls

Check these before using $round in production pipelines.

half

Half rounds to even

2.5 → 2, not 3. Do not assume “always round .5 up” like some school rules or Math.round.

float

Floating-point surprises

Doubles can store values slightly above or below what you typed. For money, prefer integer cents or Decimal128.

type

Non-numeric values error

Strings like "12.3" fail. Convert first with $toDouble / $convert, or guard with $isNumber.

place

Place must be in range

Use an integer with -20 < place < 100. Negative places round to the left of the decimal.

Performance Notes

ApproachCostBest for
$roundPer-document mathDisplay / reporting precision
+ expression inputStill O(docs)Rounding averages, rates, totals
Store rounded valuesWrite-time costHot query paths that always need N decimals

Rounding is cheap compared with large sorts or joins. Prefer keeping full precision in storage and rounding in the pipeline when you only need nicer output for reports.

Key Takeaways

  • Role: round to nearest value at a given place.
  • Place: >0 decimals, 0 integer, <0 tens/hundreds.
  • Half: rounds to the nearest even number.
  • Always up/down? use $ceil / $floor instead.

One line: $round answers “what is this number at N places?”—nearest value, with half going to even.

Frequently Asked Questions

$round rounds a number to a whole integer or to a specified decimal place. Use it inside aggregation expression stages such as $project and $addFields.
Use { $round: [ <number>, <place> ] }. place is optional and defaults to 0 (round to an integer). place must be an integer with -20 < place < 100.
Positive place rounds after the decimal (2 → two decimals). place 0 rounds to an integer. Negative place rounds to the left of the decimal (-1 → tens, -2 → hundreds).
When the deciding digit is exactly 5, $round uses round-half-to-even (banker's rounding). For example, 10.5 and 11.5 round to 10 and 12 at place 0.
If the number is null or the field is missing, $round returns null. NaN returns NaN. Infinity stays Infinity.
$round goes to the nearest value (with half-to-even). $ceil always goes up; $floor always goes down. $trunc chops toward zero without rounding.

Next: $rtrim

Remove trailing characters from a string.

$rtrim tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful