MongoDB $sin Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Trigonometry

What You’ll Learn

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.

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.

📝 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

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (trigonometry)
Syntax{ $sin: <expression> }
Input unitRadians (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

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:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "degrees": 30 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "degrees": 45 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "degrees": 60 }
]

Example 1 — $sin with $degreesToRadians

Convert each angle to radians, then compute its sine:

mongosh
db.angles.aggregate([
  {
    $project: {
      degrees: 1,
      radians: { $degreesToRadians: "$degrees" },
      sine: {
        $sin: { $degreesToRadians: "$degrees" }
      }
    }
  }
])

How It Works

  • $degreesToRadians converts the degree field to radians.
  • $sin computes the sine of that radian value.
  • 30° → 0.5, 45° → ≈ 0.7071, 60° → ≈ 0.8660.

📈 Practical Patterns

Work with radian fields, recover unit-circle coordinates, and validate boundary values.

Example 2 — Basic $sin on a Radians Field

When data is already stored in radians, apply $sin directly:

mongosh
db.angles.aggregate([
  {
    $project: {
      radians: 1,
      sine: { $sin: "$radians" }
    }
  }
])

// radians: 0   → sine: 0
// radians: 1   → sine: ≈ 0.8415
// radians: 1.57 → sine: ≈ 1

How It Works

Skip conversion when your collection already uses radians. This is common in physics and engineering datasets.

Example 3 — y-Component on the Unit Circle

On a unit circle, the y-coordinate equals sin(θ). Compute it from a stored angle:

mongosh
db.vectors.aggregate([
  {
    $project: {
      label: 1,
      angleRadians: 1,
      xComponent: { $cos: "$angleRadians" },
      yComponent: { $sin: "$angleRadians" }
    }
  }
])

How It Works

Pair $sin with $cos to recover both coordinates from an angle. This pattern appears in robotics, game physics, and circular motion models.

Example 4 — $sin with Literal Values

Common boundary values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      sinZero:  { $sin: 0 },                    // 0
      sinPi6:   { $sin: 0.5235987755982988 },   // ≈ 0.5  (π/6 = 30°)
      sinPi2:   { $sin: 1.5707963267948966 },   // ≈ 1    (π/2 = 90°)
      sinPi:    { $sin: 3.141592653589793 }     // ≈ 0    (π = 180°)
    }
  }
])

How It Works

$sin(0) = 0, $sin(π/2) = 1, and $sin(π) = 0. These edge cases help validate pipeline logic and unit tests.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about $sin

Use these points when computing sines in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $sinh for hyperbolic sine, or review $degreesToRadians for angle conversion.

Next: $sinh →

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.

5 people found this page helpful