In MongoDB, the $radiansToDegrees
aggregation pipeline operator converts an input value measured in radians to degrees.
$radiansToDegrees
accepts any valid expression that resolves to a number.
The $radiansToDegrees
operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 0.5 }
And let’s assume that the value of the data
field is in radians (i.e. 0.5
radians).
We can use the $radiansToDegrees
operator to convert the data
field to degrees:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
radians: "$data",
degrees: { $radiansToDegrees: "$data" }
}
}
]
)
Result:
{ "radians" : 0.5, "degrees" : 28.64788975654116 }
By default, the $radiansToDegrees
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 $radiansToDegrees
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 $radiansToDegrees
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
radians: "$data",
degrees: { $radiansToDegrees: "$data" }
}
}
]
).pretty()
Result:
{ "radians" : NumberDecimal("0.1301023541559787031443874490659"), "degrees" : NumberDecimal("7.454315797853905125952127312900524") }
The output is 128-bit decimal.
Null Values
Null values return null
when using the $radiansToDegrees
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $radiansToDegrees
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
degrees: { $radiansToDegrees: "$data" }
}
}
]
)
Result:
{ "degrees" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $radiansToDegrees
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
degrees: { $radiansToDegrees: 0 * "$data" }
}
}
]
)
Result:
{ "degrees" : NaN }
Infinity
If the argument resolves to Infinity
or -Infinity
, the $radiansToDegrees
operator returns Infinity
.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : Infinity }
Let’s run $radiansToDegrees
against the data field:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
degrees: { $radiansToDegrees: "$data" }
}
}
]
)
Result:
{ "degrees" : Infinity }
Non-Existent Fields
If the $radiansToDegrees
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
degrees: { $radiansToDegrees: "$name" }
}
}
]
)
Result:
{ "degrees" : null }
Combined with Other Operators
Some aggregation pipeline operators return their result in radians. You can combine $radiansToDegrees
with such operators to return the result in degrees.
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 0.5 }
And suppose we want to use the $asin
operator to return the arcsine of the data
field. The $asin
operator returns its result in radians, but suppose we want the result in degrees instead.
In this case, we can do the following:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
radians: { $asin: "$data" },
degrees: { $radiansToDegrees: { $asin: "$data" } }
}
}
]
)
Result:
{ "radians" : 0.5235987755982988, "degrees" : 29.999999999999996 }
In this example, the first field presents the result in radians, and the second field presents it in degrees.