Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB $floor Operator

Updated on Nov 24, 2024
By Mari Selvan
👁️ 28 - Views
⏳ 4 mins
💬 1 Comment
MongoDB $floor Operator

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $floor operator serves as a powerful tool for rounding down numerical values to the nearest integer or specified decimal place. This operator is invaluable for various data manipulation tasks, especially when dealing with numerical data that requires precision and consistency.

Let's delve into the details of how the $floor operator functions within MongoDB's aggregation pipelines.

💡 Syntax

The syntax for the $floor method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $floor: <expression> }
  • $floor: This operator signifies that the subsequent operation will round down the numerical value.
  • <expression>: This represents the expression that evaluates to the numerical value to be rounded down.

📝 Example

⌨️ Input

Consider a collection named temperatures containing documents with fields location and temperature representing temperature data in various locations. Here are sample documents from the temperatures collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York", "temperature": 23.5 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "temperature": 31.8 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago", "temperature": 17.2 }
]

🔄 Aggregation

Suppose we want to round down the temperature values to the nearest integer. Here's how you can achieve this using the $floor operator:

example.js
Copied
Copy To Clipboard
db.temperatures.aggregate([
  {
    $project: {
      location: 1,
      roundedTemperature: { $floor: "$temperature" }
    }
  }
])

🧩 Explanation

  • $project: This stage reshapes documents by including or excluding fields. Here, we include the location field and create a new field roundedTemperature.
  • $floor: Rounds down the temperature field to the nearest integer.

When discussing how the above aggregation works:

  • For the first document, with "New York" as the location and a temperature of 23.5, the $floor operation rounds down 23.5 to 23, so the "roundedTemperature" becomes 23.
  • For the second document, with "Los Angeles" as the location and a temperature of 31.8, the $floor operation rounds down 31.8 to 31, so the "roundedTemperature" becomes 31.
  • For the third document, with "Chicago" as the location and a temperature of 17.2, the $floor operation rounds down 17.2 to 17, so the "roundedTemperature" becomes 17.

💻 Output

Now, let's take a look at the output generated by the aggregation pipeline:

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York", "roundedTemperature": 23 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "roundedTemperature": 31 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago", "roundedTemperature": 17 }

📚 Use Cases

  1. Data Normalization:

    The $floor operator is useful for normalizing numerical data, ensuring consistency and uniformity within datasets.

  2. Statistical Analysis:

    When performing statistical computations, rounding down temperature values can aid in summarizing data and identifying trends.

  3. Visualization:

    Rounded temperature values are often easier to interpret and visualize in graphs or charts, enhancing data presentation and understanding.

🎉 Conclusion

The $floor operator in MongoDB's aggregation framework provides a convenient solution for rounding down numerical values within aggregation pipelines. Whether you're normalizing data, performing statistical analysis, or enhancing data visualization, mastering the usage of $floor empowers you to efficiently manipulate numerical data and derive meaningful insights from your datasets.

With its intuitive syntax and versatile applications, the $floor operator proves to be an indispensable asset for handling numerical data effectively within MongoDB. Incorporate it into your aggregation pipelines to streamline data processing and unlock new dimensions of data analysis.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy