MongoDB $radiansToDegrees Operator

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

What You’ll Learn

The $radiansToDegrees operator converts an angle from radians to degrees in MongoDB aggregation pipelines. Use it when math stages produce radians but you need readable degree values for dashboards, APIs, or reports.

01

Radians → Degrees

Convert angle units in pipelines.

02

Syntax

One expression inside $radiansToDegrees.

03

Display Ready

Human-readable angle output.

04

Formula

radians × (180 / π).

05

Use Cases

Geospatial, sensors, reports.

06

Inverse

Pair with $degreesToRadians.

Definition and Usage

In MongoDB’s aggregation framework, the $radiansToDegrees operator converts a numeric radian value into degrees. For example, π radians becomes 180 degrees, and π/2 radians becomes 90 degrees. This is the inverse of $degreesToRadians and is essential when trig operators like $asin or $acos return radians that users expect to see in degrees.

💡
Beginner Tip

Think of $radiansToDegrees as the “display” step after math. MongoDB trig functions work in radians internally; convert back to degrees before showing angles on a map, chart, or API response.

📝 Syntax

The $radiansToDegrees operator takes one numeric expression (an angle in radians):

mongosh
{ $radiansToDegrees: <expression> }

Syntax Rules

  • $radiansToDegrees — multiplies the input by 180/π and returns degrees.
  • <expression> — can be a field path ("$bearingRad"), 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 radian values convert to negative degrees (valid for direction or rotation).

📐 Conversion Formula: radians × (180 / π)

MongoDB applies the standard math formula. Common radian values convert predictably:

$radiansToDegrees: π (≈ 3.142) → 180°
$radiansToDegrees: π/2 (≈ 1.571) → 90°
$radiansToDegrees: (≈ 6.283) → 360°

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (math / unit conversion)
Syntax{ $radiansToDegrees: <expression> }
InputAngle in radians (any numeric value)
Output unitDegrees
Inverse operator$degreesToRadians
Example
{
  $radiansToDegrees: 3.14159
}

≈ 180° (π rad)

Field
{
  $radiansToDegrees: "$bearingRad"
}

Convert stored radians

Right angle
{
  $radiansToDegrees: 1.5708
}

≈ 90° (π/2 rad)

Null input
{
  $radiansToDegrees: null
}

Returns null

Examples Gallery

Start with angles stored in radians, convert them to degrees for display, and chain with inverse trig operators.

📚 Radian to Degree Conversion

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

Sample Input Documents

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

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

Example 1 — Basic $radiansToDegrees on a Field

Convert each bearing from radians to degrees:

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

How It Works

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

📈 Practical Patterns

Convert inverse trig results, add display fields, and validate common angle values.

Example 2 — Convert $acos Output to Degrees

Inverse trig operators return radians. Convert for human-readable output:

mongosh
db.triangles.aggregate([
  {
    $project: {
      sideA: 1,
      sideB: 1,
      sideC: 1,
      angleRad: {
        $acos: {
          $divide: [
            {
              $subtract: [
                { $add: [
                  { $pow: [ "$sideA", 2 ] },
                  { $pow: [ "$sideB", 2 ] }
                ]},
                { $pow: [ "$sideC", 2 ] }
              ]
            },
            { $multiply: [ 2, "$sideA", "$sideB" ] }
          ]
        }
      },
      angleDeg: {
        $radiansToDegrees: {
          $acos: {
            $divide: [
              {
                $subtract: [
                  { $add: [
                    { $pow: [ "$sideA", 2 ] },
                    { $pow: [ "$sideB", 2 ] }
                  ]},
                  { $pow: [ "$sideC", 2 ] }
                ]
              },
              { $multiply: [ 2, "$sideA", "$sideB" ] }
            ]
          }
        }
      }
    }
  }
])

How It Works

$acos returns radians. Wrap the result with $radiansToDegrees so dashboards and APIs show familiar degree values like 60° instead of 1.047 radians.

Example 3 — Add Display Fields with $addFields

Keep radian values for math but add degree fields for reporting:

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

How It Works

Preserve precise radian fields for calculations while adding degree fields that operators and end users can read at a glance.

Example 4 — $radiansToDegrees with Literal Values

Common radian values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      deg0:   { $radiansToDegrees: 0 },      // 0°
      deg90:  { $radiansToDegrees: 1.5708 }, // π/2 → 90°
      deg180: { $radiansToDegrees: 3.14159 },// π → 180°
      deg360: { $radiansToDegrees: 6.28319 } // 2π → 360°
    }
  }
])

How It Works

0 rad → 0°, π/2 rad → 90°, π rad → 180°, and 2π rad → 360°. Use these to validate pipeline logic or sanity-check conversions.

Bonus — Round Trip with $degreesToRadians

Convert degrees to radians and back to verify the inverse relationship:

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

// restoredDeg should equal bearingDeg (within floating-point precision)

How It Works

$degreesToRadians followed by $radiansToDegrees reconstructs the original degree value. The two operators are mathematical inverses.

🚀 Use Cases

  • API and dashboard output — convert pipeline radian results to degrees for end users.
  • Inverse trig results — display $acos, $asin, or $atan output in degrees.
  • Geospatial reporting — transform computed bearings from radians into compass-friendly degrees.
  • Data validation — round-trip with $degreesToRadians to verify angle conversions in ETL pipelines.

🧠 How $radiansToDegrees Works

1

MongoDB reads the radian value

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

Input
2

$radiansToDegrees applies the formula

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

Transform
3

The result is stored in the pipeline

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

Output
=

Degrees ready for display

Human-readable angles for maps, charts, APIs, and operator dashboards.

Conclusion

The $radiansToDegrees operator converts radian angles to degrees in MongoDB aggregation pipelines. It is the display counterpart to $degreesToRadians and is essential when trig math produces radians but users expect degree values.

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

💡 Best Practices

✅ Do

  • Use $radiansToDegrees after $acos, $asin, or $atan for display
  • Keep radian fields when precision math is needed downstream
  • Test with 0, π/2, π, and 2π to validate pipeline logic
  • Pair with $degreesToRadians for round-trip conversions
  • Handle null inputs with $ifNull when needed

❌ Don’t

  • Assume trig output is already in degrees
  • Mix up $radiansToDegrees with $degreesToRadians
  • Use $radiansToDegrees as a query filter outside expressions
  • Forget that null input returns null
  • Pass degree values into $radiansToDegrees by mistake

Key Takeaways

Knowledge Unlocked

Five things to remember about $radiansToDegrees

Use these points when converting angles in MongoDB pipelines.

5
Core concepts
📝 02

Simple Syntax

{ $radiansToDegrees: expr }

Syntax
🛠 03

Display Output

After inverse trig ops.

Usage
🌎 04

Inverse Pair

$degreesToRadians.

Pairing
05

Null Aware

null in → null out.

Edge case

❓ Frequently Asked Questions

$radiansToDegrees converts an angle from radians to degrees. Use it when pipeline math produces radians but you need human-readable degree values for display, reports, or APIs.
The syntax is { $radiansToDegrees: <expression> }. The expression can be a field reference like "$bearingRad", a literal number such as 3.14159, or another numeric expression.
MongoDB applies degrees = radians × (180 / π). For example, π radians becomes 180 degrees, and π/2 radians becomes 90 degrees.
$degreesToRadians converts degrees to radians. The two operators are inverses: one multiplies by π/180, the other multiplies by 180/π.
$radiansToDegrees returns null when the input is null. It does not throw an error.

Continue the Operator Series

Move on to $rand for random values, or review $degreesToRadians for the inverse conversion.

Next: $rand →

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