MongoDB Aggregation
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:
{ $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:
[
{ "_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:
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:
{ "_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
Geometric Computations:
The
$sin
operator facilitates computations involving angles, such as determining distances or orientations in geometric applications.Physics and Engineering:
When modeling physical systems or designing engineering structures, sine functions are often used to represent periodic phenomena or oscillatory behavior.
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:
Author
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
If you have any doubts regarding this article (MongoDB $sin Operator), please comment here. I will help you immediately.