$degreesToRadians (aggregation)
Definition
New in version 4.2.
Converts an input value measured in degrees to radians.
$degreesToRadians
has the following syntax:
- { $degreesToRadians: <expression> }
$degreesToRadians
takes any valid expression that resolves to a number.
By default $degreesToRadians
returns values as adouble
. $degreesToRadians
can also return valuesas a 128-bit decimal as long as the<expression>
resolves to a 128-bit decimal value.
For more information on expressions, seeExpressions.
Behavior
null, NaN, and +/- Infinity
If the argument resolves to a value of null
or refers to a fieldthat is missing, $degreesToRadians
returns null
. Ifthe argument resolves to NaN
, $degreesToRadians
returns NaN
. If the argument resolves to negative or positiveinfinity, $degreesToRadians
negative or positive infinityrespectively.
Example | Results |
---|---|
{ $degreesToRadians: NaN } | NaN |
{ $degreesToRadians: null } | null |
{ $degreesToRadians : Infinity} | Infinity |
{ $degreesToRadians : -Infinity } | -Infinity |
Example
The trigonometry
collection contains a document that containsthree angles measured in degrees:
- {
- "angle_a" : NumberDecimal("53.13010235415597870314438744090659"),
- "angle_b" : NumberDecimal("36.86989764584402129685561255909341"),
- "angle_c" : NumberDecimal("90")
- }
The following aggregation operation uses the$degreesToRadians
expression to convert each value toits radian equivalent and add them to the input document using the$addFields
pipeline stage.
- db.trigonometry.aggregate([
- {
- $addFields: {
- "angle_a_rad" : { $degreesToRadians : "$angle_a"},
- "angle_b_rad" : { $degreesToRadians : "$angle_b"},
- "angle_c_rad" : { $degreesToRadians : "$angle_c"}
- }
- }
- ])
The operation returns the following document:
- {
- "_id" : ObjectId("5c50aec71c75c59232b3ede4"),
- "angle_a" : NumberDecimal("53.13010235415597870314438744090660"),
- "angle_b" : NumberDecimal("36.86989764584402129685561255909341"),
- "angle_c" : NumberDecimal("90"),
- "angle_a_rad" : NumberDecimal("0.9272952180016122324285124629224290"),
- "angle_b_rad" : NumberDecimal("0.6435011087932843868028092287173227"),
- "angle_c_rad" : NumberDecimal("1.570796326794896619231321691639752")
- }
Since angle_a
, angle_b
, and angle_c
are stored as128-bit decimals, the output of$degreesToRadians
is a 128-bit decimal.