![]() |
VOOZH | about |
In MongoDB, the $ceil operator is a powerful tool used in aggregation pipelines to round numbers up to the nearest integer greater than or equal to the original number. In this article, We will learn about the MongoDB $ceil Operator in detail.
MongoDB$ceil operator is used in aggregation pipelines to perform mathematical rounding of a number to the smallest integer greater than or equal to that number. It is similar to the CEIL function in many programming languages and SQL.
{ $ceil: <number> }
Here, the number is a valid expression until it resolves to a number.
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
Output:
👁 ImageIn this example, we are going to find the smallest integer greater than or equal to the value of the perfoPoint field in the development department.
db.employee.aggregate([{$match: {department: "Development"}},
{$project: {perfoPoint: 1, ceilingPoint: {$ceil: "$perfoPoint"}}}])
Output:
👁 ImageIn this example, we are going to find the smallest integer greater than or equal to the value of the perfoPoint.firstMonthPoint field in the HR department.
db.employee.aggregate([{$match: {department: "HR"}},
... {$project: {"perfoPoint.firstMonthPoint": 1,
ceilingPoint: {$ceil: "$perfoPoint.firstMonthPoint"}}}])
Output:
The $ceil operator in MongoDB is a valuable tool for rounding numbers up to the nearest integer. It can be used in aggregation pipelines to perform mathematical operations on numeric fields, ensuring data accuracy and consistency. By understanding how to use $ceil effectively, you can enhance your MongoDB queries and streamline your data processing tasks.