In MongoDB, the $degreesToRadians aggregation pipeline operator converts an input value measured in degrees to radians.
$degreesToRadians accepts any valid expression that resolves to a number.
The $degreesToRadians operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test with the following document:
{ "_id" : 1, "data" : 180 }
And let’s assume that the value of the data field is in degrees (therefore, in this case it’s 180 degrees).
We can use the $degreesToRadians operator to convert the data field to radians:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
degrees: "$data",
radians: { $degreesToRadians: "$data" }
}
}
]
)
Result:
{ "degrees" : 180, "radians" : 3.141592653589793 }
By default, the $degreesToRadians operator returns values as a double, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.
128-Bit Decimal Values
If the expression provided to $degreesToRadians is 128-bit decimal, then the result is returned in 128-bit decimal.
Suppose we add the following document to our collection:
{ "_id" : 2, "data" : NumberDecimal("0.1301023541559787031443874490659") }
Let’s run the the $degreesToRadians operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
degrees: "$data",
radians: { $degreesToRadians: "$data" }
}
}
]
).pretty()
Result:
{
"degrees" : NumberDecimal("90.1301023541559787031443874490659"),
"radians" : NumberDecimal("1.573067041239514175890278047405623")
}
The output is 128-bit decimal.
Null Values
Null values return null when using the $degreesToRadians operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $degreesToRadians operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
radians: { $degreesToRadians: "$data" }
}
}
]
)
Result:
{ "radians" : null }
We can see that the result is null.
NaN Values
If the argument resolves to NaN, $degreesToRadians returns NaN.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
radians: { $degreesToRadians: 0 * "$data" }
}
}
]
)
Result:
{ "radians" : NaN }
Infinity
If the argument resolves to Infinity or -Infinity, the $degreesToRadians operator returns Infinity.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : Infinity }
Let’s run $degreesToRadians against the data field:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
radians: { $degreesToRadians: "$data" }
}
}
]
)
Result:
{ "radians" : Infinity }
Non-Existent Fields
If the $degreesToRadians operator is applied against a field that doesn’t exist, null is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
radians: { $degreesToRadians: "$name" }
}
}
]
)
Result:
{ "radians" : null }
Combined with Other Operators
Some aggregation pipeline operators accept their expressions in radians. If a field has its value in degrees, you can combine $degreesToRadians to convert the expression to radians.
Suppose we have a collection called test with the following document:
{ "_id" : 1, "data" : 3 }
And in this case, the data field’s value is in degrees.
Now suppose we want to use the $cosh operator to return the hyperbolic cosine of the data field. The only problem is that the $cosh operator accepts expressions in radians.
No problem! We can use $degreesToRadians to convert the input expression to radians.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $degreesToRadians: { $cosh: "$data" } }
}
}
]
)
Result:
{ "hyperbolicCosine" : 0.17571384980422547 }