Confirm the angle is in radians (or convert with $degreesToRadians).
Pass that expression to $sin.
Optionally multiply by a hypotenuse to get an opposite side length.
Piece
Required?
Role
$sin
Yes
Sine of radians → number in [-1, 1]
Expression
Yes
Radians value, field, or nested math
$degreesToRadians
No
Convert degrees before $sin
Pseudocode
Pseudocode
for each document:
x = evaluate(expression) // expect radians
if x is null or missing:
result = null
else if x is NaN:
result = NaN
else:
result = sin(x) // in [-1, 1]
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing sine expressions.
Simulates { $sin: radians } (same idea as the mongosh examples below).
Result
radians = π/6 ≈ 0.523599
$sin → 0.5
{ sine: 0.5 }
Trace
Worked Walkthrough — Opposite Side
A right triangle has angle π/6 (30°) and hypotenuse 10. What is the opposite side?
Calculation
Document:
{ angle_rad: π/6, hypotenuse: 10 }
sine = $sin("$angle_rad")
= sin(π/6)
= 0.5
opposite = sine × hypotenuse
= 0.5 × 10
= 5
Check: in a 30°–60°–90° triangle, opposite to 30° is half the hypotenuse ✓
Code
MongoDB Examples
We use one triangle document. First we store radians and a hypotenuse, then we compute sine, then we find the opposite side. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one right triangle with angle π/6 radians (30°) and hypotenuse 10. We save those as one document in a collection named triangles.
Trig expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need a sine. Prefer radians for math stages and convert degrees only when the source data needs it.
Remember
Key Takeaways
Role: sine of an angle in radians.
Syntax:{ $sin: "$radians" }.
Units: convert degrees with $degreesToRadians first.
Triangle tip: opposite = sin(θ) × hypotenuse.
One line:$sin answers “what is the sine of this radian angle?”—in aggregation, like Math.sin.
Frequently Asked Questions
$sin returns the sine of an angle measured in radians. The result is a number between -1 and 1. Use it inside aggregation expression stages such as $project and $addFields.
The syntax is { $sin: <expression> }. The expression can be a field like "$radians", a literal number, or another numeric expression that resolves to radians.
$sin expects radians. If your data stores degrees, convert first with $degreesToRadians before passing the value to $sin.
$sin takes an angle in 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 or the field is missing. If the argument is NaN, $sin returns NaN.
Yes for the math. Think of $sin as MongoDB’s aggregation version of Math.sin—run it in the pipeline, not as a standalone find() filter.