Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB $round Operator

Updated on Nov 24, 2024
By Mari Selvan
👁️ 52 - Views
⏳ 4 mins
💬 1 Comment
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:

syntax.js
Copied
Copy To Clipboard
{ $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:

Output
Copied
Copy To Clipboard
[
  { "_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:

example.js
Copied
Copy To Clipboard
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:

Output
Copied
Copy To Clipboard
{ "_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

  1. Data Presentation:

    The $round operator is valuable for presenting numerical data with a specified level of precision, ensuring clarity and readability.

  2. Financial Calculations:

    When dealing with financial data, rounding values to a certain number of decimal places is often necessary to maintain accuracy.

  3. 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:

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