MongoDB $degreesToRadians Operator

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

What Is $degreesToRadians?

$degreesToRadians is a MongoDB aggregation expression that converts an angle from degrees to radians. Use it before $sin, $cos, or $tan, which expect radians.

Remember
$degreesToRadians = degrees × (π / 180)

{ $degreesToRadians: <number expression> }

  0°   → 0
 90°   → π/2 ≈ 1.570796
180°   → π   ≈ 3.141593
360°   → 2π  ≈ 6.283185
-90°   → -π/2

null / missing → null
NaN → NaN
±Infinity → ±Infinity
UsePrefer
Degrees → radians$degreesToRadians
Radians → degrees$radiansToDegrees
Sine / cosine after convert$sin / $cos

How to Use $degreesToRadians in MongoDB

Pass a degree value, get radians, then feed that into trig if needed.

  1. Choose an expression stage ($addFields or $project).
  2. Pass a number expression (field or literal) to $degreesToRadians.
  3. Store or use the radian result.
  4. Optionally wrap with $sin / $cos / $tan for trig on degree input.
PieceRequired?Role
$degreesToRadiansYesDegrees → radians
ExpressionYesNumber, field, or nested math
$sin / $cosNoTrig after conversion

Pseudocode

Pseudocode
for each document:
    deg = evaluate(expression)
    if deg is null or missing:
        result = null
    else if deg is NaN:
        result = NaN
    else if deg is ±Infinity:
        result = ±Infinity
    else:
        result = deg * (π / 180)
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while converting angles.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $degreesToRadians: <number> }
Formuladegrees × π / 180
Common check90° → π/2 ≈ 1.5708
Inverse$radiansToDegrees
Trig pattern{ $sin: { $degreesToRadians: "$deg" } }
Return typeUsually double (Decimal128 stays Decimal128)

Live Preview

Watch $degreesToRadians convert a degree angle to radians. Try 90 to see π/2.

Simulates $degreesToRadians (same idea as the mongosh examples below).

Result
degrees = 90
$degreesToRadians → 1.570796 rad

{ degrees: 90, radians: 1.570796 }

Worked Walkthrough — Why 90° Is π/2

A full circle is 360° or 2π radians. Half of a right angle’s circle share makes the conversion clear.

Calculation
Full circle:
  360° = 2π radians

So:
  1° = 2π / 360 = π / 180 radians

For 90°:
  radians = 90 × (π / 180)
          = π / 2
          ≈ 1.57079632679

Check with sine (radians):
  sin(π/2) = 1

In MongoDB:
  { $sin: { $degreesToRadians: 90 } } → 1

MongoDB Examples

We store three angles in degrees, convert them to radians, then take $sin of 90° after conversion. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine a tiny angles collection with three familiar degree values: 0, 90, and 180.

mongosh
db.angles.insertMany([
  { _id: 1, label: "zero",     degrees: 0 },
  { _id: 2, label: "right",    degrees: 90 },
  { _id: 3, label: "straight", degrees: 180 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in angles. Run find() to see them:

mongosh
db.angles.find()

Read those documents like this:

Field in MongoDBValuePlain English
_id1 … 3Document id for this lesson
label"zero", "right", "straight"Friendly name for the angle
degrees0, 90, 180Angle we will convert to radians
Picture
Expected radians:

  0°   → 0
  90°  → π/2 ≈ 1.5708
 180°  → π   ≈ 3.1416

How It Works

  1. Insert once. insertMany adds three angle documents to angles.
  2. Values are degrees. Humans often store bearings as degrees; MongoDB trig wants radians.
  3. find() shows the same shape. Ready for $degreesToRadians next.

Example 2 — Convert to Radians

Goal: add a radians field for each stored degree value.

mongosh
db.angles.aggregate([
  {
    $addFields: {
      radians: { $degreesToRadians: "$degrees" }
    }
  }
]);

How It Works

  1. Read degrees. Each document already has degrees from Example 1.
  2. Multiply by π/180. $degreesToRadians turns 90 into about 1.5708.
  3. Keep originals. $addFields adds radians next to degrees.
  4. Easy check. 180° should be about 3.1416 (π).

Example 3 — $sin After Convert

Practical pattern: degrees in storage, radians for trig. sin(90°) should be 1.

mongosh
db.angles.aggregate([
  { $match: { degrees: 90 } },
  {
    $project: {
      label: 1,
      degrees: 1,
      sine: {
        $sin: {
          $degreesToRadians: "$degrees"
        }
      },
      _id: 0
    }
  }
]);

How It Works

  1. Filter the right angle. $match keeps the 90° document.
  2. Convert first. $degreesToRadians turns 90 into π/2.
  3. Take sine in radians. $sin of π/2 is 1.
  4. Easy check. If you forgot the convert step and ran $sin: 90, you would get the wrong answer.

Edge Cases & Pitfalls

Check these before using $degreesToRadians in production pipelines.

units

Trig needs radians

Do not pass raw degrees to $sin / $cos / $tan. Convert with $degreesToRadians first.

null

Null and missing become null

Missing fields return null, not an error. Handle them before trig if nulls are invalid in your data.

range

Any real number is allowed

Values outside 0–360 still convert (for example 450° → 450 × π/180). Normalize only if your domain requires it.

inverse

Going back to degrees

Use $radiansToDegrees. Do not multiply by π/180 again.

Performance Notes

ApproachCostBest for
$degreesToRadians alonePer-document multiplyUnit conversion reports
+ $sin / $cosStill O(docs)Trig on degree-stored fields
Store radians insteadExtra schema workHot paths that convert every request

Unit conversion is cheap compared with large sorts or joins. Filter early with $match when only some documents need trig.

Key Takeaways

  • Role: degrees → radians (× π/180).
  • Check: 90° → π/2 ≈ 1.5708.
  • Trig: convert before $sin / $cos / $tan.
  • Inverse: $radiansToDegrees.

One line: $degreesToRadians answers “what is this degree angle in radians?”—so MongoDB trig can use it.

Frequently Asked Questions

$degreesToRadians converts an angle from degrees to radians. MongoDB trig operators like $sin, $cos, and $tan expect radians, so convert first when your data is stored in degrees.
radians = degrees × (π / 180). Examples: 180° → π ≈ 3.14159, 90° → π/2 ≈ 1.5708, 0° → 0.
The syntax is { $degreesToRadians: <expression> }. The expression can be a field, a literal number, or nested numeric math.
Yes. Negative degrees become negative radians. For example, -90° becomes about -1.5708 radians.
$degreesToRadians returns null when the input is null or the field is missing. NaN returns NaN. ±Infinity returns ±Infinity.
Use $radiansToDegrees, which multiplies by 180/π. That is the inverse of $degreesToRadians.

Next: $divide

Divide two numbers in an aggregation pipeline.

$divide 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