Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB $atan Operator

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

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $atan operator offers a powerful tool for performing trigonometric computations on numerical data. By computing the arctangent (inverse tangent) of a given number, this operator enables a wide range of applications in fields such as geometry, physics, and engineering.

Let's dive into the details of how the $atan operator can be effectively utilized within MongoDB's aggregation pipelines.

💡 Syntax

The syntax for the $atan method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $atan: <expression> }
  • $atan: This operator indicates that the subsequent operation will involve computing the arctangent.
  • <expression>: This represents the numerical expression for which the arctangent will be calculated. It could be a field reference, a mathematical expression, or a value.

📝 Example

⌨️ Input

Consider a collection named vectors containing documents with fields x and y, representing vector components. Here are sample documents from the vectors collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "x": 3, "y": 4 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "x": -2, "y": 2 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "x": 1, "y": -1 }
]

🔄 Aggregation

Suppose we want to calculate the angle (in radians) of each vector relative to the x-axis. Here's how you can achieve this using the $atan operator:

example.js
Copied
Copy To Clipboard
db.vectors.aggregate([
  {
    $project: {
      angleRadians: { $atan: { $divide: ["$y", "$x"] } }
    }
  }
])

🧩 Explanation

  • $project: This stage reshapes documents to include computed fields.
  • $divide: This divides the y component by the x component to compute the slope of the vector.
  • $atan: Computes the arctangent of the result, yielding the angle (in radians) relative to the x-axis.

When discussing how the above aggregation works:

  1. For the first document:
    • x: 3 and y: 4
    • angleRadians: atan(4/3) ≈ 0.93 radians
  2. For the second document:
    • x: -2 and y: 2
    • angleRadians: atan(2/-2) ≈ -0.79 radians
  3. For the third document:
    • x: 1 and y: -1
    • angleRadians: atan(-1/1) ≈ -0.79 radians

💻 Output

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

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "angleRadians": 0.93 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "angleRadians": -0.79 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "angleRadians": -0.79 }

📚 Use Cases

  1. Geometric Computations:

    The $atan operator facilitates angle calculations, making it useful for various geometric applications.

  2. Engineering Analysis:

    When analyzing vectors or orientations in engineering projects, the $atan operator aids in precise computations.

  3. Trigonometric Analysis:

    MongoDB's $atan operator supports trigonometric analysis in scientific and engineering domains, contributing to tasks such as signal processing and data visualization.

🎉 Conclusion

The $atan operator in MongoDB's aggregation framework offers a valuable solution for computing arctangents within aggregation pipelines. Whether you're analyzing geometric data, engineering simulations, or scientific experiments, mastering the usage of $atan enhances your ability to perform accurate trigonometric computations and gain deeper insights into your datasets.

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

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