The $sin operator computes the sine of an angle in MongoDB aggregation pipelines. Use it for geometry, signal processing, and physics calculations when your data stores angles in radians.
01
Sine
Angle → ratio.
02
Syntax
One expression inside $sin.
03
Radians
Input must be radians.
04
Output Range
Always -1 to 1.
05
Use Cases
Geometry, signals, physics.
06
vs $asin
Forward vs inverse.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $sin operator returns the sine of a numeric angle expressed in radians. For example, sin(0) = 0, sin(π/2) = 1, and sin(π/6) = 0.5 (30°). The result is always between -1 and 1.
💡
Beginner Tip
$sin expects radians, not degrees. Convert with $degreesToRadians or multiply by π / 180 before applying $sin.
Foundation
📝 Syntax
The $sin operator takes one numeric expression (angle in radians):
mongosh
{ $sin: <expression> }
Syntax Rules
$sin — returns the sine of the angle in radians.
<expression> — a field reference, literal, or nested numeric expression.
Output range is always -1 to 1 (inclusive).
Use inside stages like $project, $addFields, or $set.
If the input is null, the result is null.
Input must be in radians, not degrees.
⚠️ Input Must Be in Radians
$sin(0) → 0(0 radians = 0°)
$sin(1.571) → ≈ 1(π/2 rad = 90°)
$sin(30) → wrong if you meant 30° — convert degrees first!
Formula: radians = degrees × (π / 180) or use $degreesToRadians
💡 $sin vs $asin vs $sinh
$sin — angle (radians) → sine value (-1 to 1)
$asin — sine value (-1 to 1) → angle (radians)
$sinh — hyperbolic sine (different function, not standard trig)
$asin($sin(x)) ≈ x within the valid domain
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (trigonometry)
Syntax
{ $sin: <expression> }
Input unit
Radians (any real number)
Output range
-1 to 1
Common stages
$project, $addFields, $set
Zero angle
{
$sin: 0
}
Returns 0
Field
{
$sin: "$radians"
}
Sine of angle field
π/2
{
$sin: 1.5707963267948966
}
Returns 1 (90°)
From degrees
{
$sin: {
$degreesToRadians: 30
}
}
Returns 0.5
Hands-On
Examples Gallery
Compute sines from stored degree values, convert with $degreesToRadians, and explore common boundary angles with $sin.
📚 Sine from Degrees
Use an angles collection with degree measurements and compute sine with $degreesToRadians.
Sample Input Documents
Suppose you have an angles collection where each document stores an angle in degrees:
$sin(0) = 0, $sin(π/2) = 1, and $sin(π) = 0. These edge cases help validate pipeline logic and unit tests.
Applications
🚀 Use Cases
Geometric calculations — compute heights, distances, and y-components from angles.
Signal processing — model periodic signals and sine waves in stored sensor data.
Physics and engineering — analyze oscillations, vibrations, and wave motion.
Data visualization — generate sine curves for charts and simulations inside aggregation pipelines.
🧠 How $sin Works
1
MongoDB reads the angle
The pipeline evaluates the input — a field like "$radians" or a converted degree value in radians.
Input
2
$sin applies the sine function
MongoDB computes sin(angle) and returns a value between -1 and 1.
Transform
3
The result is stored in the pipeline
The sine value is written to the field you define in $project or $addFields.
Output
=
📐
Sine ratio ready to use
Use in further math, pair with $cos, or feed into inverse ops like $asin.
Wrap Up
Conclusion
The $sin operator brings standard sine math directly into MongoDB aggregation pipelines. Remember that input angles must be in radians, and the output always falls between -1 and 1.
For geometry and physics workloads, pair $sin with $cos to recover full coordinates. Use $degreesToRadians when your data stores degrees. Next in the series: $sinh.
Store angles in radians or convert degrees with $degreesToRadians
Pair with $cos for full unit-circle coordinates
Use literal boundary tests (0, π/2, π) to validate pipelines
Remember output is always between -1 and 1
Use $asin for the inverse when recovering angles
❌ Don’t
Pass degree values directly to $sin without conversion
Confuse $sin (angle → ratio) with $asin (ratio → angle)
Expect exact floating-point equality — compare with tolerance
Forget that null input returns null
Mix up $sin (trig) with $sinh (hyperbolic sine)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $sin
Use these points when computing sines in MongoDB.
5
Core concepts
📐01
Sine
Angle → ratio.
Purpose
📝02
{ $sin: x }
One expression.
Syntax
📏03
Radians In
Not degrees.
Rule
🛠04
-1 to 1
Output range.
Property
⚠05
Not $asin
Forward vs inverse.
Important
❓ Frequently Asked Questions
$sin returns the sine of an angle. The input must be in radians, and the output is a number between -1 and 1. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $sin: <expression> }. The expression can be a field reference like "$radians", a literal number, or another numeric expression.
$sin expects radians. If your data stores degrees, convert first with $degreesToRadians or multiply by π/180 using $multiply and $divide before passing the value to $sin.
$sin takes an angle (radians) and returns a sine value between -1 and 1. $asin takes a sine value (-1 to 1) and returns an angle in radians. They are inverse operations.
$sin returns null when the input is null. It does not throw an error.