$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.

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $ceil: <numeric-expression> }

⚙️ Parameters

numeric valueA 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏