$atan2用来计算反正切,返回指定表达式的反正切值,与$antan的区别主要是参数不同。
语法
{ $atan2: [
使用
如果任一参数解析为null,则结果返回null;如果任一参数解析为NaN,则结果返回NaN;如果一个参数解析为null,另一个解析为NaN,则结果返回null,如下表所示:
示例结果{ $atan2: [NaN,
举例
trigonometry集合中有下面的文档,包含了直角三角形的三条边:
{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5")
}
反正切角度
下面的聚合操作使用$atan2表达式计算与边side_a相邻的角度,然后使用$radiansToDegrees运算符将结果转换为度数,最后使用$addFields管道阶段将其添加到输入文档。
db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$radiansToDegrees : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
}
])
执行的结果为:
{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("53.13010235415597870314438744090658")
}
反正切弧度
与上例不同,在求反正切后,没有进行度数的转换,结果为弧度:
db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
])
结果为:
{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("0.9272952180016122324285124629224287")
}
相关文章
发表评论