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.
Fundamentals
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.
Foundation
📝 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
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (rounding)
Syntax
{ $ceil: <expression> }
Direction
Up (toward +∞)
Output
Integer (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
Hands-On
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:
Choose $ceil when you must never under-charge or under-allocate. Choose $floor to always round down, and $round for standard nearest-integer behavior.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $ceil
Use these points when rounding numbers in MongoDB.
5
Core concepts
🔢01
Round Up
Toward +∞ always.
Purpose
📝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.