Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB $tan Operator

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

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $tan operator serves as a valuable tool for computing the tangent of a given angle. This operator facilitates various trigonometric calculations, enabling users to analyze and manipulate numerical data effectively.

Let's delve into the details of how the $tan operator can be utilized within MongoDB's aggregation pipelines.

💡 Syntax

The syntax for the $tan method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $tan: <angle> }
  • $tan: This operator signifies that the subsequent operation will compute the tangent of the specified angle.
  • <angle>: This represents the angle (in radians) for which the tangent will be calculated. It could be a field reference, a mathematical expression, or a value.

📝 Example

⌨️ Input

Consider a collection named angles containing documents with fields degrees representing angles in degrees. Here are sample documents from the angles collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "degrees": 30 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "degrees": 45 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "degrees": 60 }
]

🔄 Aggregation

Suppose we want to compute the tangent of each angle in radians. Here's how you can achieve this using the $tan operator:

example.js
Copied
Copy To Clipboard
db.angles.aggregate([
  {
    $project: {
      tangent: { $tan: { $degreesToRadians: "$degrees" } }
    }
  }
])

🧩 Explanation

  • $degreesToRadians: This operator converts degrees to radians, as the $tan operator expects angles in radians.
  • $tan: Computes the tangent of the angle (in radians).

When discussing how the above aggregation works:

  1. For the document with "_id" 609c26812e9274a86871bc6a and "degrees" equal to 30:
    • Convert 30 degrees to radians: 30 * (π / 180) ≈ 0.5236 radians
    • Calculate the tangent of 0.5236 radians: tan(0.5236) ≈ 0.5774
  2. For the document with "_id" 609c26812e9274a86871bc6b and "degrees" equal to 45:
    • Convert 45 degrees to radians: 45 * (π / 180) ≈ 0.7854 radians
    • Calculate the tangent of 0.7854 radians: tan(0.7854) ≈ 1
  3. For the document with "_id" 609c26812e9274a86871bc6c and "degrees" equal to 60:
    • Convert 60 degrees to radians: 60 * (π / 180) ≈ 1.0472 radians
    • Calculate the tangent of 1.0472 radians: tan(1.0472) ≈ 1.7321

💻 Output

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

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "tangent": 0.5774 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "tangent": 1 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "tangent": 1.7321 }

📚 Use Cases

  1. Geometry and Trigonometry:

    The $tan operator is essential for performing trigonometric computations, such as calculating slopes, angles, and distances in geometric applications.

  2. Engineering and Physics:

    Tangent values are frequently used in engineering simulations, physics experiments, and mathematical modeling.

  3. Data Analysis:

    Trigonometric functions play a significant role in various data analysis tasks, such as signal processing, time-series analysis, and spatial data analysis.

🎉 Conclusion

The $tan operator in MongoDB's aggregation framework provides a convenient means of computing tangents within aggregation pipelines, facilitating a wide range of trigonometric calculations. Whether you're working with geometric data, engineering simulations, or mathematical models, mastering the usage of $tan empowers you to analyze and manipulate numerical data effectively within MongoDB.

With its intuitive syntax and versatile applications, the $tan operator proves to be a valuable asset for handling numerical data within MongoDB's aggregation framework. Incorporate it into your aggregation pipelines to unlock new possibilities in 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