MongoDB $sin Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $sin?

$sin is a MongoDB aggregation expression that returns the sine of an angle measured in radians. The result is a number between -1 and 1.

Remember
$sin = Math.sin for aggregation

{ $sin: <expression> }

input  → radians (not degrees)
output → number in [-1, 1]

{ $sin: 0 }           → 0
{ $sin: π/2 }         → 1
{ $sin: π/6 }         → 0.5

degrees → wrap with $degreesToRadians first
null / missing → null
UsePrefer
Sine of an angle$sin
Degrees → radians first$degreesToRadians
Cosine / inverse sine$cos / $asin

How to Use $sin in MongoDB

Pass an angle in radians (convert from degrees if needed), then use the sine value.

  1. Choose an expression stage ($addFields or $project).
  2. Confirm the angle is in radians (or convert with $degreesToRadians).
  3. Pass that expression to $sin.
  4. Optionally multiply by a hypotenuse to get an opposite side length.
PieceRequired?Role
$sinYesSine of radians → number in [-1, 1]
ExpressionYesRadians value, field, or nested math
$degreesToRadiansNoConvert 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 }

Cheat Sheet

Keep these facts handy while writing sine expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $sin: "$radians" }
UnitsInput must be radians
Output range[-1, 1]
Common anglessin(0)=0, sin(π/6)=0.5, sin(π/2)=1
Degrees pattern{ $sin: { $degreesToRadians: "$deg" } }
Triangle tipopposite = sin(θ) × hypotenuse
Related$cos, $tan, $asin

Live Preview

Watch $sin evaluate common radian angles.

Simulates { $sin: radians } (same idea as the mongosh examples below).

Result
radians = π/6 ≈ 0.523599

$sin → 0.5

{ sine: 0.5 }

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 ✓

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.

mongosh
db.triangles.insertOne({
  _id: 1,
  label: "30 degree corner",
  angle_rad: Math.PI / 6,
  hypotenuse: 10
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in triangles. Run find() to see it:

mongosh
db.triangles.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
label"30 degree corner"Friendly name
angle_radπ/6 ≈ 0.5236Angle in radians
hypotenuse10Longest side of the triangle
Picture
Right triangle (easy 30° corner):

        |\
        | \
  opp   |  \   10  (hypotenuse)
        |   \
        |    \
        |_____\
        angle = π/6 (30°)

We will find the opposite side:
  sin(θ) = opposite / hypotenuse
  opposite = sin(θ) × hypotenuse
           = 0.5 × 10
           = 5

How It Works

  1. Insert once. insertOne adds a single triangle document to triangles.
  2. Store radians as a number. Math.PI / 6 becomes a double in MongoDB.
  3. find() shows the same shape. What you inserted is what you get back—ready for $sin next.

Example 2 — Compute Sine

Goal: add a sine field from angle_rad.

mongosh
db.triangles.aggregate([
  {
    $addFields: {
      sine: { $sin: "$angle_rad" }
    }
  }
]);

How It Works

  1. Read the stored radians. MongoDB already has angle_rad ≈ 0.5236 (π/6) from Example 1.
  2. Apply $sin. sin(π/6) is 0.5 (floating point may show as 0.4999…).
  3. Keep all fields. $addFields adds sine next to the original data—nothing is deleted.

Example 3 — Opposite Side

Multiply sine by the hypotenuse to get the side opposite the angle. If your angle were in degrees, nest $degreesToRadians inside $sin.

mongosh
db.triangles.aggregate([
  {
    $project: {
      label: 1,
      angle_rad: 1,
      hypotenuse: 1,
      opposite: {
        $multiply: [
          { $sin: "$angle_rad" },
          "$hypotenuse"
        ]
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using π/6 and hypotenuse 10 from Example 1.
  2. Sine first. $sin of π/6 is about 0.5.
  3. Scale by hypotenuse. $multiply does 0.5 × 10 ≈ 5.
  4. Easy check. Opposite to 30° is half the hypotenuse—so 5 looks right.

Edge Cases & Pitfalls

Check these before using $sin in production pipelines.

units

Input must be radians

Passing 30 (degrees) is wrong. Convert with $degreesToRadians first.

null

Null or missing → null

Missing fields return null, not 0. Handle them before trig if nulls are invalid.

float

Floating-point noise

sin(π/6) may print as 0.4999… instead of exactly 0.5. Round for display if needed.

inverse

Need the angle from a sine?

Use $asin, not $sin. They are inverse operations.

Performance Notes

ApproachCostBest for
$sin aloneCheap per documentAngle fields already in radians
+ $degreesToRadiansStill lightweightDegree-stored angles
+ $multiplyOne extra multiplyOpposite-side triangle math

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.

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.

Next: $sinh

Compute the hyperbolic sine of a numeric value.

$sinh tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful