MongoDB $ceil Operator

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

What You’ll Learn

The $ceil operator rounds a number up to the nearest integer in MongoDB aggregation pipelines. It is MongoDB’s version of Math.ceil() and is useful whenever partial values must count as a full unit.

01

Round Up

Always toward +∞.

02

Syntax

One expression inside $ceil.

03

$project Stage

Add computed fields easily.

04

Negatives

ceil(-2.3) = -2

05

Use Cases

Billing, charts, counts.

06

vs $floor

Up vs down rounding.

Definition and Usage

In MongoDB’s aggregation framework, the $ceil operator returns the smallest integer greater than or equal to the input. For example, $ceil(23.5) returns 24, and $ceil(14.2) returns 15. Integers pass through unchanged: $ceil(5) returns 5.

💡
Beginner Tip

Use $ceil when you need to always round up — like charging for a full shipping box when an order slightly exceeds capacity. For nearest-integer rounding, use $round instead.

📝 Syntax

The $ceil operator takes one numeric expression:

mongosh
{ $ceil: <expression> }

Syntax Rules

  • $ceil — rounds the expression up to the nearest integer.
  • <expression> — can be a field path, a literal number, or another numeric expression.
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
  • Rounds toward positive infinity (see negative number examples below).

💡 $ceil vs $floor vs $round

All three round numbers, but in different directions:

Value: 23.5$ceil: 24, $floor: 23, $round: 24
Value: 23.4$ceil: 24, $floor: 23, $round: 23
Value: -2.3$ceil: -2, $floor: -3, $round: -2

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (rounding)
Syntax{ $ceil: <expression> }
DirectionUp (toward +∞)
OutputInteger (or null)
Common stages$project, $addFields, $set
Positive
{
  $ceil: 23.5
}

Returns 24

Negative
{
  $ceil: -2.3
}

Returns -2

Integer
{
  $ceil: 5
}

Returns 5

Null input
{
  $ceil: null
}

Returns null

Examples Gallery

Round temperature readings up, handle negative values, and calculate billing units from partial quantities.

📚 Round Up Values

Use a temperatures collection and round readings up to whole numbers with $project.

Sample Input Documents

Suppose you have a temperatures collection with decimal readings:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York",    "temperature": 23.5 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "temperature": 18.9 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago",     "temperature": 14.2 }
]

Example 1 — Basic $ceil on a Field

Round up temperature readings to the nearest integer:

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

How It Works

  • New York: ceil(23.5)24.
  • Los Angeles: ceil(18.9)19.
  • Chicago: ceil(14.2)15.

📈 Practical Patterns

Handle negative numbers, calculate billing units, and compare rounding operators side by side.

Example 2 — $ceil with Negative Numbers

$ceil rounds toward positive infinity, not away from zero:

mongosh
db.readings.aggregate([
  {
    $project: {
      value: 1,
      ceiled:  { $ceil: "$value" },
      floored: { $floor: "$value" },
      rounded: { $round: "$value" }
    }
  }
])

// value: -2.3 → ceiled: -2, floored: -3, rounded: -2
// value: -2.0 → ceiled: -2, floored: -2, rounded: -2
// value:  2.3 → ceiled:  3, floored:  2, rounded:  2

How It Works

For negatives, $ceil moves right on the number line toward zero. This surprises beginners who expect -2.3 to become -3 — that is $floor.

Example 3 — Calculate Billing Units (Round Up)

When items ship in boxes of 10, any partial box counts as a full box:

mongosh
db.orders.aggregate([
  {
    $project: {
      orderId: 1,
      quantity: 1,
      boxesNeeded: {
        $ceil: { $divide: [ "$quantity", 10 ] }
      }
    }
  }
])

// quantity: 25 → boxesNeeded: 3
// quantity: 30 → boxesNeeded: 3
// quantity: 31 → boxesNeeded: 4

How It Works

$divide gives a decimal. $ceil ensures you never under-count partial units — a classic pattern in logistics and billing pipelines.

Example 4 — Compare $ceil, $floor, and $round

See all three rounding operators on the same field:

mongosh
db.samples.aggregate([
  {
    $project: {
      price: 1,
      ceilPrice:  { $ceil: "$price" },
      floorPrice: { $floor: "$price" },
      roundPrice: { $round: "$price" }
    }
  }
])

// price: 19.99 → ceil: 20, floor: 19, round: 20
// price: 19.49 → ceil: 20, floor: 19, round: 19
// price: 19.00 → ceil: 19, floor: 19, round: 19

How It Works

Choose $ceil when you must never under-charge or under-allocate. Choose $floor to always round down, and $round for standard nearest-integer behavior.

🚀 Use Cases

  • Data visualization — round up readings for cleaner charts and dashboards.
  • Statistical analysis — ceiling operations for discrete counts and bucket sizing.
  • Financial calculations — round up currency amounts when partial cents must count as a full unit.
  • Logistics and billing — calculate boxes, pages, or API calls needed for partial quantities.

🧠 How $ceil Works

1

MongoDB reads the number

The pipeline evaluates the input — a field like "$temperature" or a numeric expression.

Input
2

$ceil rounds up

MongoDB returns the smallest integer ≥ the input, moving toward positive infinity.

Transform
3

The result is stored in the pipeline

The integer is written to the field you define in $project or $addFields.

Output
=

Whole-number result

Use the rounded value for billing, reporting, or downstream calculations.

Conclusion

The $ceil operator is a straightforward rounding tool in MongoDB aggregation pipelines. It always rounds up toward positive infinity, which makes it ideal for billing, capacity planning, and any scenario where partial units must count as a full unit.

For beginners, remember: ceil(-2.3) = -2 (not -3), integers pass through unchanged, and pair with $divide for box-or-batch calculations.

💡 Best Practices

✅ Do

  • Use $ceil when partial units must count as full units
  • Test with negative decimals to verify expected behavior
  • Combine with $divide for box/batch calculations
  • Compare with $floor and $round before choosing
  • Handle null inputs with $ifNull when needed

❌ Don’t

  • Assume $ceil rounds away from zero on negatives
  • Use $ceil when nearest-integer rounding is enough
  • Confuse $ceil with $round or $floor
  • Use $ceil as a query filter outside expressions
  • Forget that ceil(5.0) returns 5 unchanged

Key Takeaways

Knowledge Unlocked

Five things to remember about $ceil

Use these points when rounding numbers in MongoDB.

5
Core concepts
📝 02

Simple Syntax

{ $ceil: expr }

Syntax
📏 03

Negatives

ceil(-2.3) = -2.

Edge case
🛠 04

Billing Pattern

ceil(qty / size).

Pattern
05

Not $round

Up, not nearest.

Important

❓ Frequently Asked Questions

$ceil rounds a number up to the nearest integer (toward positive infinity). For example, $ceil applied to 23.5 returns 24.
The syntax is { $ceil: <expression> }. The expression can be a field reference like "$temperature", a literal number, or another numeric expression.
$ceil rounds toward positive infinity. For example, ceil(-2.3) returns -2, not -3. This is different from rounding away from zero.
$ceil rounds up, $floor rounds down, and $round rounds to the nearest integer. Use $ceil when you always need to round up (e.g., billing for partial units).
$ceil returns null when the input is null. It does not throw an error.

Continue the Operator Series

Move on to $cmp for value comparison, or review $round for standard rounding.

Next: $cmp →

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