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.
Fundamentals
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.
Foundation
📝 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π)
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (math / unit conversion)
Syntax
{ $degreesToRadians: <expression> }
Input
Angle in degrees (any numeric value)
Output unit
Radians
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
Hands-On
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:
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.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $degreesToRadians
Use these points when converting angles in MongoDB pipelines.
5
Core concepts
📐01
Degrees to Radians
Multiply by π/180.
Purpose
📝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.