MongoDB $round Operator
Photo Credit to CodeToFun
🙋 Introduction
Within MongoDB's aggregation framework, the $round
operator offers a convenient way to round numerical values to a specified number of decimal places. This operator is particularly useful in scenarios where precision is required or when presenting data in a more concise format.
Let's delve into how the $round
operator can be effectively utilized within MongoDB's aggregation pipelines.
💡 Syntax
The syntax for the $round
method is straightforward:
{ $round: { <expression>, <decimalPlaces> } }
- $round: This operator signifies that the subsequent operation will round a numerical value.
- <expression>: This represents the numerical expression or field reference to be rounded.
- <decimalPlaces>: This specifies the number of decimal places to which the value should be rounded.
📝 Example
⌨️ Input
Consider a collection named temperatures containing documents with fields location and temperature representing temperature readings. Here are sample documents from the temperatures collection:
[
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York", "temperature": 21.578 },
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "temperature": 18.942 },
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago", "temperature": 16.723 }
]
🔄 Aggregation
Suppose we want to round the temperature values to two decimal places. Here's how you can achieve this using the $round
operator:
db.temperatures.aggregate([
{
$project: {
location: 1,
roundedTemperature: { $round: ["$temperature", 2] }
}
}
])
🧩 Explanation
- $project: This stage reshapes documents in the aggregation pipeline and includes or excludes fields as specified.
- $round: Rounds the temperature field to two decimal places.
When discussing how the above aggregation works:
- In the first document, the temperature value of 21.578 is rounded to 21.58.
- In the second document, the temperature value of 18.942 is rounded to 18.94.
- In the third document, the temperature value of 16.723 is rounded to 16.72.
💻 Output
Now, let's take a look at the output generated by the aggregation pipeline:
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York", "roundedTemperature": 21.58 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "roundedTemperature": 18.94 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago", "roundedTemperature": 16.72 }
📚 Use Cases
Data Presentation:
The
$round
operator is valuable for presenting numerical data with a specified level of precision, ensuring clarity and readability.Financial Calculations:
When dealing with financial data, rounding values to a certain number of decimal places is often necessary to maintain accuracy.
Statistical Analysis:
In statistical computations, rounding values can help in simplifying results without sacrificing meaningfulness.
🎉 Conclusion
The $round
operator in MongoDB's aggregation framework provides a straightforward solution for rounding numerical values to a specified number of decimal places. Whether you're presenting data, performing financial calculations, or conducting statistical analysis, mastering the usage of $round
empowers you to manipulate numerical data effectively within MongoDB's aggregation pipelines.
With its intuitive syntax and practical applications, the $round
operator proves to be a valuable asset for handling numerical data within MongoDB. Incorporate it into your aggregation pipelines to streamline data processing and enhance the presentation of your analysis results.
👨💻 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 $round Operator), please comment here. I will help you immediately.