MongoDB $round Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Math

What You’ll Learn

The $round operator rounds numeric values to a chosen number of decimal places inside MongoDB aggregation pipelines. Use it to clean up sensor readings, format prices, or simplify report output without changing stored data.

01

Round Numbers

Control precision.

02

place Arg

Decimals or tens.

03

Array Syntax

[ number, place ].

04

$project

Add rounded fields.

05

Use Cases

Finance, stats, UI.

06

Null Safety

Know null behavior.

Definition and Usage

In MongoDB’s aggregation framework, the $round operator rounds a numeric expression to a specified number of decimal places. For example, rounding 21.578 to two places gives 21.58, and rounding 1234 to the nearest hundred (place: -2) gives 1200.

This is especially useful when presenting data, computing financial totals, or normalizing sensor readings where full floating-point precision is unnecessary or confusing for end users.

💡
Beginner Tip

Think of $round like JavaScript’s rounding helpers, but with an explicit place argument. Positive place counts digits after the decimal; negative place rounds left of the decimal (tens, hundreds, etc.).

📝 Syntax

The $round operator accepts an array of two expressions:

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

Alternatively, you can use the object form:

mongosh
{
  $round: {
    input: <number expression>,
    place: <place expression>
  }
}

Syntax Rules

  • First operand — the number to round (field path, literal, or expression).
  • Second operand (place) — how many decimal places to keep.
  • place: 2 — round to two decimal places (e.g. 18.942 → 18.94).
  • place: 0 — round to the nearest whole number.
  • Negative place — round to tens, hundreds, etc. (-1 → nearest 10; -2 → nearest 100).
  • Use inside $project, $addFields, or $set.
  • If the number is null, the result is null.

💡 place Values at a Glance

{ $round: [21.578, 2] }21.58 (two decimals)
{ $round: [18.942, 0] }19 (nearest integer)
{ $round: [1234.56, -1] }1230 (nearest ten)
{ $round: [1234.56, -2] }1200 (nearest hundred)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (arithmetic)
Syntax{ $round: [ number, place ] }
Operand countExactly two expressions
OutputRounded number (or null)
Common stages$project, $addFields, $set
2 decimals
{
  $round: ["$price", 2]
}

Currency style

Integer
{
  $round: ["$score", 0]
}

Whole number

Nearest 100
{
  $round: ["$sales", -2]
}

place: -2

Null input
null

Returns null

Examples Gallery

Round temperature readings, format prices, round averages, and snap sales figures to the nearest hundred with $round.

📚 Sensor & Weather Data

Round temperature readings to two decimal places for clean display.

Sample Input Documents

Suppose you have a temperatures collection:

mongosh
[
  {
    "_id": 1,
    "location": "New York",
    "temperature": 21.578
  },
  {
    "_id": 2,
    "location": "Los Angeles",
    "temperature": 18.942
  },
  {
    "_id": 3,
    "location": "Chicago",
    "temperature": 16.723
  }
]

Example 1 — Round to Two Decimal Places

Clean up temperature values for a dashboard:

mongosh
db.temperatures.aggregate([
  {
    $project: {
      location: 1,
      temperature: 1,
      roundedTemperature: {
        $round: ["$temperature", 2]
      }
    }
  }
])

How It Works

The second argument 2 tells MongoDB to keep two digits after the decimal point, using standard rounding rules.

📈 Finance & Reporting

Round prices, averages, and large totals for readable reports.

Example 2 — Round Price to Whole Dollars

Display product prices without cents:

mongosh
db.products.aggregate([
  {
    $project: {
      name: 1,
      price: 1,
      priceRounded: {
        $round: ["$price", 0]
      }
    }
  }
])

// price: 49.99  → priceRounded: 50
// price: 29.4   → priceRounded: 29

How It Works

place: 0 rounds to the nearest integer. Values at exactly .5 round away from zero (e.g. 49.5 → 50).

Example 3 — Round a Computed Average

Round the result of a division expression:

mongosh
db.orders.aggregate([
  {
    $group: {
      _id: "$customerId",
      total: { $sum: "$amount" },
      count: { $sum: 1 }
    }
  },
  {
    $project: {
      avgOrder: {
        $round: [
          { $divide: ["$total", "$count"] },
          2
        ]
      }
    }
  }
])

// total: 100, count: 3 → avgOrder: 33.33

How It Works

The first argument can be any numeric expression, not just a field path. Here, $divide computes the average and $round formats it.

Example 4 — Round Sales to Nearest Hundred

Use a negative place for coarse reporting buckets:

mongosh
db.stores.aggregate([
  {
    $project: {
      store: 1,
      monthlySales: 1,
      salesRounded: {
        $round: ["$monthlySales", -2]
      }
    }
  }
])

// monthlySales: 1234.56 → salesRounded: 1200
// monthlySales: 9876.50 → salesRounded: 9900

How It Works

place: -2 rounds to the nearest hundred. Negative values move the rounding boundary left of the decimal point.

Bonus — Round Tax on a Line Item

Compute and round tax to two decimal places:

mongosh
db.invoices.aggregate([
  {
    $project: {
      item: 1,
      subtotal: 1,
      tax: {
        $round: [
          { $multiply: ["$subtotal", 0.0825] },
          2
        ]
      }
    }
  }
])

// subtotal: 19.99 → tax: 1.65 (8.25% rate)

How It Works

Multiply first, then round. This pattern avoids storing long floating-point tails in invoice output.

🚀 Use Cases

  • Data presentation — show readable numbers in dashboards and API responses.
  • Financial calculations — round currency, tax, and totals to required precision.
  • Statistical summaries — format averages, rates, and percentages cleanly.
  • Reporting buckets — snap large values to tens or hundreds for summary charts.

🧠 How $round Works

1

MongoDB evaluates both operands

The pipeline resolves the number and the place value to numbers (or null).

Input
2

The value is rounded to the target precision

Positive place rounds decimal digits; zero rounds to an integer; negative rounds to tens, hundreds, and so on.

Round
3

The rounded number is returned

The result is a numeric value suitable for display, further math, or storage in a projected field.

Output
=

Precision-controlled number

Use in projections, grouped averages, or chained with $multiply and $divide.

Conclusion

The $round operator rounds numeric values to a specified number of decimal places in MongoDB aggregation pipelines. It keeps reports readable and financial output precise without manual formatting in application code.

Remember: positive place for decimals, zero for integers, negative for tens and hundreds. Next in the series: $rtrim.

💡 Best Practices

✅ Do

  • Round at the end of calculations (multiply/divide first, then round)
  • Use place: 2 for currency-style output
  • Use negative place for coarse summary reporting
  • Handle null inputs with $ifNull when fields may be missing
  • Keep raw values in storage; round in projections for display

❌ Don’t

  • Round too early in multi-step financial calculations (rounding errors add up)
  • Assume place truncates — it rounds, not chops digits
  • Use $round as a query filter (it is expression-only)
  • Forget that null input returns null
  • Confuse rounding with $floor or $ceil (always down/up)

Key Takeaways

Knowledge Unlocked

Five things to remember about $round

Use these points when rounding numbers in pipelines.

5
Core concepts
📝 02

[ n, place ]

Two operands.

Syntax
🛠 03

place sign

+ / 0 / −.

place
🗃 04

Expressions OK

Not just fields.

Input
05

null

Null in → null out.

Edge case

❓ Frequently Asked Questions

$round rounds a numeric value to a specified number of decimal places. Positive place values round after the decimal point; zero rounds to a whole number; negative place values round to tens, hundreds, and so on.
The array syntax is { $round: [ <number>, <place> ] }. You can also use { $round: { input: <expression>, place: <expression> } }. Use inside $project, $addFields, or $set.
place is the number of decimal places. place: 2 rounds to two decimals (21.578 → 21.58). place: 0 rounds to an integer. place: -2 rounds to the nearest hundred (1234 → 1200).
Yes. The first argument can be a field path like "$price" or any numeric expression, such as { $divide: ["$total", "$count"] }.
If the number expression resolves to null, $round returns null. It does not throw an error.

Continue the Operator Series

Move on to $rtrim for trimming trailing whitespace, or review $multiply for calculations before rounding.

Next: $rtrim →

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.

5 people found this page helpful