MongoDB $degreesToRadians Operator

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

What You’ll Learn

The $degreesToRadians operator converts an angle from degrees to radians in MongoDB aggregation pipelines. Use it when your data stores angles in degrees but you need radians for trigonometry or geospatial math.

01

Degrees → Radians

Convert angle units in pipelines.

02

Syntax

One expression inside $degreesToRadians.

03

Trig Ready

Prepare angles for $sin, $cos.

04

Formula

degrees × (π / 180).

05

Use Cases

Geospatial, sensors, robotics.

06

Field References

Convert stored degree fields.

Definition and Usage

In MongoDB’s aggregation framework, the $degreesToRadians operator converts a numeric degree value into radians. For example, 180 degrees becomes approximately 3.142 radians (π), and 90 degrees becomes approximately 1.571 radians (π/2). This is essential because MongoDB trigonometric operators such as $sin, $cos, and $tan expect radians, not degrees.

💡
Beginner Tip

Think of $degreesToRadians as MongoDB’s built-in version of converting compass headings or map angles into the format required by math functions. Use it inside expression stages like $project or $addFields.

📝 Syntax

The $degreesToRadians operator takes one numeric expression (an angle in degrees):

mongosh
{ $degreesToRadians: <expression> }

Syntax Rules

  • $degreesToRadians — multiplies the input by π/180 and returns radians.
  • <expression> — can be a field path ("$bearing"), a literal number, or another numeric expression.
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
  • Negative degree values convert to negative radians (valid for direction or rotation).

📐 Conversion Formula: degrees × (π / 180)

MongoDB applies the standard math formula. Full rotations and common angles convert predictably:

$degreesToRadians: 180≈ 3.142 rad (π)
$degreesToRadians: 90≈ 1.571 rad (π/2)
$degreesToRadians: 360≈ 6.283 rad (2π)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (math / unit conversion)
Syntax{ $degreesToRadians: <expression> }
InputAngle in degrees (any numeric value)
Output unitRadians
Common stages$project, $addFields, $set
Example
{
  $degreesToRadians: 180
}

≈ 3.142 rad (π)

Field
{
  $degreesToRadians: "$bearing"
}

Convert stored degrees

Right angle
{
  $degreesToRadians: 90
}

≈ 1.571 rad (π/2)

Null input
{
  $degreesToRadians: null
}

Returns null

Examples Gallery

Start with angles stored in degrees, run an aggregation pipeline, and see the converted radian values step by step.

📚 Degree to Radian Conversion

Use a locations collection with bearing angles in degrees and convert them to radians with $project.

Sample Input Documents

Suppose you have a locations collection where each document stores a compass bearing in degrees:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "North Tower", "bearingDeg": 0 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "East Gate", "bearingDeg": 90 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "South Pier", "bearingDeg": 180 }
]

Example 1 — Basic $degreesToRadians on a Field

Convert each bearing from degrees to radians:

mongosh
db.locations.aggregate([
  {
    $project: {
      name: 1,
      bearingDeg: 1,
      bearingRad: { $degreesToRadians: "$bearingDeg" }
    }
  }
])

How It Works

  • Document 1: 0 degrees → 0 radians.
  • Document 2: 90 degrees → ≈ 1.571 radians (π/2).
  • Document 3: 180 degrees → ≈ 3.142 radians (π).

📈 Practical Patterns

Chain $degreesToRadians with trigonometry and handle common angle values in pipelines.

Example 2 — Convert Then Apply $sin

MongoDB trig functions expect radians. Convert degrees first, then compute the sine:

mongosh
db.locations.aggregate([
  {
    $project: {
      name: 1,
      bearingDeg: 1,
      bearingRad: { $degreesToRadians: "$bearingDeg" },
      sinValue: {
        $sin: { $degreesToRadians: "$bearingDeg" }
      }
    }
  }
])

How It Works

Without conversion, passing 90 directly to $sin would not give the expected result. Convert to radians first so sin(90°) correctly returns 1.

Example 3 — Normalize Sensor Readings with $addFields

Add a radian field for downstream geospatial or robotics calculations without overwriting the original degree value:

mongosh
db.sensors.aggregate([
  {
    $addFields: {
      headingRad: { $degreesToRadians: "$headingDeg" },
      pitchRad: { $degreesToRadians: "$pitchDeg" }
    }
  }
])

How It Works

Keep human-readable degree fields for dashboards, but add radian fields for math-heavy pipeline stages like distance or vector calculations.

Example 4 — $degreesToRadians with Literal Values

Common angle values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      rad0:   { $degreesToRadians: 0 },    // 0 rad
      rad90:  { $degreesToRadians: 90 },   // π/2 rad
      rad180: { $degreesToRadians: 180 },  // π rad
      rad360: { $degreesToRadians: 360 }   // 2π rad
    }
  }
])

How It Works

0° → 0, 90° → π/2, 180° → π, and 360° → 2π. Use these to validate pipeline logic or as constants in geospatial formulas.

🚀 Use Cases

  • Trigonometry pipelines — convert degree inputs before calling $sin, $cos, or $tan.
  • Geospatial and navigation — transform compass headings or map bearings stored in degrees into radians for distance formulas.
  • Robotics and sensor data — normalize pitch, yaw, and heading readings from hardware that reports degrees.
  • Data ingestion — convert user-facing angle fields during ETL so downstream math uses the correct unit.

🧠 How $degreesToRadians Works

1

MongoDB reads the degree value

The pipeline evaluates the input — a field like "$bearingDeg" or a numeric literal such as 90.

Input
2

$degreesToRadians applies the formula

MongoDB multiplies the value by π/180 to produce the equivalent angle in radians.

Transform
3

The result is stored in the pipeline

The radian value is written to the field you define in $project or $addFields.

Output
=

Radians ready for math

You can pass the result directly to trig operators or geospatial formulas that require radians.

Conclusion

The $degreesToRadians operator is a small but essential math tool in MongoDB aggregation pipelines. It bridges the gap between human-friendly degree values and the radian format required by trigonometric functions, making it invaluable for geospatial, navigation, and sensor data workflows.

For beginners, the key idea is simple: wrap any degree expression in { $degreesToRadians: ... } inside a stage like $project, and MongoDB returns the radian equivalent using the standard π/180 formula.

💡 Best Practices

✅ Do

  • Use $degreesToRadians before $sin, $cos, or $tan
  • Keep original degree fields when users need readable values
  • Test with 0, 90, 180, and 360 to validate pipeline logic
  • Pair with $radiansToDegrees for round-trip conversions
  • Handle null inputs with $ifNull when needed

❌ Don’t

  • Pass degree values directly to $sin or $cos without converting
  • Assume the output is in degrees
  • Use $degreesToRadians as a query filter outside expressions
  • Forget that null input returns null
  • Mix up $degreesToRadians with $radiansToDegrees

Key Takeaways

Knowledge Unlocked

Five things to remember about $degreesToRadians

Use these points when converting angles in MongoDB pipelines.

5
Core concepts
📝 02

Simple Syntax

{ $degreesToRadians: expr }

Syntax
🛠 03

Trig Functions

Required before $sin/$cos.

Usage
🌎 04

Geospatial Ready

Convert bearings and headings.

Use case
05

Null Aware

null in → null out.

Edge case

❓ Frequently Asked Questions

$degreesToRadians converts an angle from degrees to radians. MongoDB trig operators like $sin, $cos, and $tan expect radians, so this operator prepares human-friendly degree values for math inside aggregation pipelines.
The syntax is { $degreesToRadians: <expression> }. The expression can be a field reference like "$bearing", a literal number such as 90, or another numeric expression.
MongoDB applies radians = degrees × (π / 180). For example, 180 degrees becomes π (about 3.14159), and 90 degrees becomes π/2 (about 1.5708).
Yes. Negative degree values are valid and convert to negative radians. For example, -90 degrees becomes approximately -1.5708 radians.
$degreesToRadians returns null when the input is null. It does not throw an error.

Continue the Operator Series

Move on to $divide for division in pipelines, or review $dayOfYear for date extraction.

Next: $divide →

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