Open In App

MongoDB $floor Operator

Last Updated : 06 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The MongoDB $floor operator is a powerful tool used in the aggregation pipeline to round numbers down to the nearest integer that is less than or equal to the original number. Whether we’re working with employee performance metrics, financial data, or any numerical dataset, the $floor operator helps in rounding values down to the nearest whole number.

In this article, We will explore everything we need to know about the MongoDB $floor operator, including its syntax, use cases, and practical examples.

What is the MongoDB $floor Operator?

MongoDB $floor operator is used in aggregation pipelines to round a number down to the nearest integer that is less than or equal to the original number. It is particularly useful when we need to adjust numerical data for reporting, analysis, or formatting purposes within your aggregation pipeline.

This operator can be used with fields, constants, or any valid numerical expression in MongoDB’s aggregation framework. It’s similar to the FLOOR function you’ll find in SQL and many programming languages, such as Python and JavaScript.

Syntax: 

{ $floor: <number> }

  • expression: This is the numeric value (field, constant, or expression) that will be rounded down.

Behavior

  • If the entered value is null, then this operator will return null.
  • If the entered value is NaN, then this operator will return NaN.
  • If the entered value is a missing field, then this operator will return null.

Use Cases of the $floor Operator

  • Data normalization: Round values down to standardize numerical data (e.g., rounding financial transactions).
  • Data formatting: Round down decimal numbers for reports, dashboards, or data analytics.
  • Data transformation: Combine with other aggregation operators like $add, $subtract, $multiply, etc., to perform complex calculations.

Why Choose MongoDB’s $floor Operator?

  • The MongoDB $floor operator is widely used for numerical data processing, and it’s highly effective when handling data cleanup, transformations, and aggregation tasks.
  • It helps ensure data consistency and simplifies the application of complex operations directly within the database, enhancing the overall performance of data-driven applications.

Examples of MongoDB $floor

Let’s go through some practical examples to understand how the $floor operator works within MongoDB aggregation pipelines. 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.

Example 1: Using $floor operator

In this example, we are going to find the largest integer less than or equal to the value of the perfoPoint field in the development department.

Query:

db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {perfoPoint: 1,
floorPoint: {$floor: "$perfoPoint"}}}])

Output:

Explanation:

  • We match all employees in the Development department.
  • The $project stage is used to return the perfoPoint field and also calculate a new field floorPoint, which stores the rounded-down value of perfoPoint.

Example 2: Using $floor operator in the embedded document

In this example, we are going to find the largest integer less than or equal to the value of the details.perfoPoint field in the HR department. Here, the field is inside an embedded document.

Query:

db.employee.aggregate([{$match: {department: "HR"}},
... {$project: {"details.perfoPoint": 1,
floorPoint: {$floor: "$details.perfoPoint"}}}])

Output:

Using-floor-operator-in-the-embedded-document

Explanation:

  • We filter employees from the HR department.
  • The $floor operator is applied to the details.perfoPoint field, which is inside an embedded document, and the result is stored in floorPoint.

Conclusion

The $floor operator in MongoDB is a valuable tool for rounding down numbers in aggregation pipelines, providing developers with the ability to perform complex calculations and formatting operations on numerical data. Its functionality is similar to the FLOOR function in other programming languages and SQL, making it a familiar and powerful tool for developers working with MongoDB databases.



Next Article

Similar Reads