Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB $sin Operator

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

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $sin operator emerges as a powerful tool for performing trigonometric computations on numerical data. This operator computes the sine of a given angle, facilitating various applications in fields such as geometry, physics, and engineering.

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

💡 Syntax

The syntax for the $sin method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $sin: <angle> }
  • $sin: This operator signifies that the subsequent operation will compute the sine of the specified angle.
  • <angle>: This represents the angle (in radians) for which the sine 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 sine of each angle in radians. Here's how you can achieve this using the $sin operator:

example.js
Copied
Copy To Clipboard
db.angles.aggregate([
  {
    $project: {
      radians: { $degreesToRadians: "$degrees" },
      sine: { $sin: { $degreesToRadians: "$degrees" } }
    }
  }
])

🧩 Explanation

  • $project: This stage adds new fields to each document in the pipeline, representing radians and the sine of the angle.
  • $degreesToRadians: This operator converts degrees to radians, as the $sin operator expects angles in radians.
  • $sin: Computes the sine of the angle (in radians).

When discussing how the above aggregation works:

  • For the first document with degrees equal to 30, its radians value is approximately 0.5236 radians, and its sine value is approximately 0.5.
  • For the second document with degrees equal to 45, its radians value is approximately 0.7854 radians, and its sine value is approximately 0.7071.
  • For the third document with degrees equal to 60, its radians value is approximately 1.0472 radians, and its sine value is approximately 0.8660.

💻 Output

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

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "radians": 0.5235987755982988, "sine": 0.49999999999999994 }
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "radians": 0.7853981633974483, "sine": 0.7071067811865476 }
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "radians": 1.0471975511965976, "sine": 0.8660254037844386 }

📚 Use Cases

  1. Geometric Computations:

    The $sin operator facilitates computations involving angles, such as determining distances or orientations in geometric applications.

  2. Physics and Engineering:

    When modeling physical systems or designing engineering structures, sine functions are often used to represent periodic phenomena or oscillatory behavior.

  3. Signal Processing:

    In signal analysis and processing, sine functions play a fundamental role in representing and manipulating periodic signals.

🎉 Conclusion

The $sin operator in MongoDB's aggregation framework provides a powerful tool for computing sine values within aggregation pipelines. Whether you're analyzing geometric data, modeling physical systems, or processing signals, mastering the usage of $sin empowers you to perform advanced trigonometric computations and gain deeper insights into your datasets.

With its intuitive syntax and diverse applications, the $sin operator proves to be an invaluable asset for handling numerical data effectively within MongoDB. Incorporate it into your aggregation pipelines to unlock new dimensions of data analysis and explore the rich possibilities of trigonometry within MongoDB.

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