$ceil (Ceiling)
Last updated: 2026-10-09
$ceil (Ceiling)
$ceil returns the smallest integer greater than or equal to a given number — rounding up. It is used for page math or capacity estimation where you must round up, such as 7.5 items requiring 8 in stock. A null or missing argument yields null, and integer input passes through unchanged. It is the opposite of $floor and differs from $trunc on negatives: -1.2 becomes -1 with $ceil, -1 with $trunc, and -2 with $floor.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $ceil: <numeric-expression> }
⚙️ Parameters
| numeric value | A numeric value or an expression resolving to one. |
|---|
Returns:The smallest integer greater than or equal to the input; null when null or missing
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $ceil: round boxes up with 12 items per box
db.orders.aggregate([
{ $project: {
boxes: { $ceil: { $divide: ['$quantity', 12] } }
} }
])
Output:
{ '_id': 1, 'boxes': 1 }
{ '_id': 2, 'boxes': 2 }
📌 Notes
- On negatives it goes the opposite way from $floor: $ceil(-1.2) is -1 while $floor(-1.2) is -2
- Integer input passes through unchanged
- Null or missing arguments yield null
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $ceil and $floor?
A$ceil rounds up and $floor rounds down; $ceil(-1.2) is -1 while $floor(-1.2) is -2.
QHow does $ceil handle negative numbers?
AIt still rounds up toward positive infinity: -1.2 becomes -1 and -2.8 becomes -2.
QCan $ceil keep a specified number of decimal places?
ANo — $ceil only produces integers; use the second argument of $round or $trunc to keep decimals.