MongoDB Aggregation
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:
{ $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:
[
{ "_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:
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:
{ "_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
Data Normalization:
The
$floor
operator is useful for normalizing numerical data, ensuring consistency and uniformity within datasets.Statistical Analysis:
When performing statistical computations, rounding down temperature values can aid in summarizing data and identifying trends.
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:
Author
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
If you have any doubts regarding this article (MongoDB $floor Operator), please comment here. I will help you immediately.