$asin聚合运算符返回反正弦函数值。
语法
{ $asin: <expression> }
$asin接受可解析为数值-1到1之间数值的表达式,即:-1 <= value <= 1$asin返回值为弧度,可以使用$radiansToDegrees运算符把弧度转换为角度。- 缺省情况下,
$asin返回双精度数double,如果<expression>表达式解析为128-bit decimal,则$asin也返回128-bit decimal。
使用
如果参数被解析为null或表达式引用的字段不存在,返回null,如果参数解析为NaN,返回NaN。如果参数表达式解析后的值超出[-1,1]的范围,将抛出异常。
| 例子 | 返回 |
|---|---|
{ $asin: NaN } | NaN |
{ $asin: null } | null |
{ $asin : 正无穷}或{ $asin : 负无穷 } | 抛出异常 |
举例
反正弦角度
trigonometry集合有下面的文档,包含了直角三角形的三个边:
{"_id" : ObjectId("5c50782193f833234ba90d85"),"side_a" : NumberDecimal("3"),"side_b" : NumberDecimal("4"),"hypotenuse" : NumberDecimal("5")
}
下面的聚合操作先使用$asin表达式计算边side_a的对角,然后使用$radiansToDegress表达式将$asin返回的弧度值转换为角度,并使用$addFeilds管道阶段将其添加到输入文档:
db.trigonometry.aggregate([{$addFields : {"angle_a" : {$radiansToDegrees : {$asin : {$divide : [ "$side_a", "$hypotenuse" ]}}}}}
])
命令运行的结果如下:
{"_id" : ObjectId("5c50782193f833234ba90d85"),"side_a" : NumberDecimal("3"),"side_b" : NumberDecimal("4"),"hypotenuse" : NumberDecimal("5"),"angle_a" : NumberDecimal("36.86989764584402129685561255909341")
}
反正弦弧度
使用上例中的集合数据和聚合命令,去掉$radiansToDegrees表达式:
db.trigonometry.aggregate([{$addFields : {"angle_a" : {$asin : {$divide : [ "$side_a", "$hypotenuse" ]}}}}
])
执行后的结果为:
{"_id" : ObjectId("5c50782193f833234ba90d85"),"side_a" : NumberDecimal("3"),"side_b" : NumberDecimal("4"),"hypotenuse" : NumberDecimal("5"),"angle_a" : NumberDecimal("0.6435011087932843868028092287173226")
}