Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB Aggregation

MongoDB $sqrt Operator

Updated on Oct 31, 2024
By Mari Selvan
👁️ 18 - Views
⏳ 4 mins
💬 1 Comment
MongoDB $sqrt Operator

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $sqrt operator plays a vital role in calculating the square root of numerical values. This operator is incredibly useful in scenarios where you need to perform mathematical operations or compute distances based on geometric data.

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

💡 Syntax

The syntax for the $sqrt method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $sqrt: <expression> }
  • $sqrt: This operator signifies that the subsequent operation will compute the square root.
  • <expression>: This represents the numerical value for which the square root will be calculated. It could be a field reference, a mathematical expression, or a value.

📝 Example

⌨️ Input

Consider a collection named numbers containing documents with fields value representing numerical values. Here are sample documents from the numbers collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "value": 9 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "value": 16 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "value": 25 }
]

🔄 Aggregation

Suppose we want to compute the square root of each value in the collection. Here's how you can achieve this using the $sqrt operator:

example.js
Copied
Copy To Clipboard
db.numbers.aggregate([
  {
    $project: {
      sqrtValue: { $sqrt: "$value" }
    }
  }
])

🧩 Explanation

  • $project: This stage reshapes documents, including, excluding, or adding new fields.
  • $sqrt: Computes the square root of the specified numerical value.

When discussing how the above aggregation works:

  • For the first document with _id "609c26812e9274a86871bc6a" and "value" of 9, the square root of 9 is 3, so "sqrtValue" becomes 3.
  • Similarly, for the second document with _id "609c26812e9274a86871bc6b" and "value" of 16, the square root of 16 is 4, so "sqrtValue" becomes 4.
  • For the third document with _id "609c26812e9274a86871bc6c" and "value" of 25, the square root of 25 is 5, so "sqrtValue" becomes 5.

💻 Output

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

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "sqrtValue": 3 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "sqrtValue": 4 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "sqrtValue": 5 }

📚 Use Cases

  1. Geometric Calculations:

    The $sqrt operator is invaluable for computing distances or dimensions in geometric calculations.

  2. Data Normalization:

    Square roots can be used to normalize numerical data or transform values within a dataset.

  3. Statistical Analysis:

    When analyzing data distributions or variability, calculating square roots can provide valuable insights.

🎉 Conclusion

The $sqrt operator in MongoDB's aggregation framework serves as a powerful tool for computing square roots within aggregation pipelines. Whether you're performing geometric calculations, data normalization, or statistical analysis, mastering the usage of $sqrt empowers you to efficiently manipulate numerical data and extract meaningful insights from your datasets.

With its intuitive syntax and diverse applications, the $sqrt operator proves to be a valuable asset for handling numerical data effectively within MongoDB. Incorporate it into your aggregation pipelines to unlock new dimensions of data analysis and gain deeper insights into your datasets.

👨‍💻 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