Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB Aggregation

MongoDB $atan2 Operator

Updated on Oct 30, 2024
By Mari Selvan
👁️ 15 - Views
⏳ 4 mins
💬 1 Comment
MongoDB $atan2 Operator

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $atan2 operator is a powerful tool for computing the arctangent of the quotient of its arguments, providing a solution for computing angles or directions from two given coordinates. This operator is particularly useful in scenarios involving spatial data analysis, such as mapping or navigation applications.

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

💡 Syntax

The syntax for the $atan2 method is straightforward:

syntax.js
Copied
Copy To Clipboard
 $atan2: [ <y>, <x> ] }
  • $atan2: This operator signifies that the subsequent operation will compute the arctangent of the quotient of its arguments.
  • <y>: This represents the numerator in the division operation.
  • <x>: This represents the denominator in the division operation.

📝 Example

⌨️ Input

Consider a collection named coordinates containing documents with fields latitude and longitude, representing geographical coordinates. Here are sample documents from the coordinates collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "latitude": 40.7128, "longitude": -74.0060 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "latitude": 34.0522, "longitude": -118.2437 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "latitude": 51.5074, "longitude": -0.1278 }
]

🔄 Aggregation

Suppose we want to calculate the angle (in radians) of each coordinate relative to the origin (0,0). Here's how you can achieve this using the $atan2 operator:

example.js
Copied
Copy To Clipboard
db.coordinates.aggregate([
  {
    $project: {
      angleRadians: { $atan2: ["$latitude", "$longitude"] }
    }
  }
])

🧩 Explanation

  • $project: This stage reshapes documents, including or excluding fields, or adding new fields.
  • $atan2: Computes the arctangent of the quotient of the latitude and longitude values, yielding the angle (in radians) relative to the origin.

Let's break down what happens with each document:

  1. For the first document:
    • Latitude: 40.7128
    • Longitude: -74.0060
    • atan2(40.7128, -74.0060): This calculates the angle in radians between the point (40.7128, -74.0060) and the positive x-axis.
  2. For the second document:
    • Latitude: 34.0522
    • Longitude: -118.2437
    • atan2(34.0522, -118.2437): This calculates the angle in radians between the point (34.0522, -118.2437) and the positive x-axis.
  3. For the third document:
    • Latitude: 51.5074
    • Longitude: -0.1278
    • atan2(51.5074, -0.1278): This calculates the angle in radians between the point (51.5074, -0.1278) and the positive x-axis.

💻 Output

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

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "angleRadians": 2.6250207174305165 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "angleRadians": 2.859769829781501 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "angleRadians": 1.606211758611045 }

📚 Use Cases

  1. Geospatial Analysis:

    The $atan2 operator is invaluable for calculating angles or directions between geographical coordinates, facilitating geospatial analysis tasks.

  2. Navigation Systems:

    In navigation applications, determining the direction or bearing between two points is crucial for route planning and guidance.

  3. Mapping Applications:

    Mapping software often requires the computation of angles or directions between coordinates to display accurate representations of geographical features.

🎉 Conclusion

The $atan2 operator in MongoDB's aggregation framework provides a robust solution for computing angles or directions from two given coordinates. Whether you're working on geospatial analysis, navigation systems, or mapping applications, mastering the usage of $atan2 enables you to perform accurate computations and gain valuable insights from spatial data within MongoDB aggregation pipelines.

With its straightforward syntax and diverse applications, the $atan2 operator proves to be an indispensable tool for handling spatial data effectively within MongoDB. Incorporate it into your aggregation pipelines to unlock new dimensions of geospatial analysis and enhance the functionality of your applications.

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