$radiansToDegrees (aggregation)

Definition

  • $radiansToDegrees

New in version 4.2.

Converts an input value measured in radians to degrees.

$radiansToDegrees has the following syntax:

  1. { $radiansToDegrees: <expression> }

$radiansToDegrees takes any valid expression that resolves to a number.

By default $radiansToDegrees returns values as adouble. $radiansToDegrees 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, $radiansToDegrees returns null. Ifthe argument resolves to NaN, $radiansToDegreesreturns NaN. If the argument resolves to negative or positiveinfinity, $radiansToDegrees negative or positive infinityrespectively.

ExampleResults
{ $radiansToDegrees: NaN }NaN
{ $radiansToDegrees: null }null
{ $radiansToDegrees : Infinity}Infinity
{ $radiansToDegrees : -Infinity }-Infinity

Example

The trigonometry collection contains a document that containsthree angles measured in radians:

  1. {
  2. "angle_a" : NumberDecimal("0.9272952180016122324285124629224290"),
  3. "angle_b" : NumberDecimal("0.6435011087932843868028092287173227"),
  4. "angle_c" : NumberDecimal("1.570796326794896619231321691639752")
  5. }

The following aggregation operation uses the$radiansToDegrees expression to convert each value toits degree equivalent and add them to the input document using the$addFields pipeline stage.

  1. db.trigangles.aggregate([
  2. {
  3. $addFields: {
  4. "angle_a_deg" : { $radiansToDegrees : "$angle_a"},
  5. "angle_b_deg" : { $radiansToDegrees : "$angle_b"},
  6. "angle_c_deg" : { $radiansToDegrees : "$angle_c"}
  7. }
  8. }
  9. ])

The operation returns the following document:

  1. {
  2. "_id" : ObjectId("5c50aec71c75c59232b3ede4"),
  3. "angle_a" : NumberDecimal("0.9272952180016122324285124629224290"),
  4. "angle_b" : NumberDecimal("0.6435011087932843868028092287173227"),
  5. "angle_c" : NumberDecimal("1.570796326794896619231321691639752"),
  6. "angle_a_deg" : NumberDecimal("53.13010235415597870314438744090659"),
  7. "angle_b_deg" : NumberDecimal("36.86989764584402129685561255909341"),
  8. "angle_c_deg" : NumberDecimal("90.00000000000000000000000000000000")
  9. }

Since angle_a, angle_b, and angle_c are stored as128-bit decimals, the output of$radiansToDegrees is a 128-bit decimal.