函数$asin (聚合)
$asin返回值的反正弦(反正切)。
$asin具有以下语法{ $asin: <expression> } $asin接受任何有效的表达式,该表达式解析为介于-1和1之间的数字,例如-1 <= value <= 1。$asin返回的值以弧度为单位。使用$radiansToDegrees操作符将输出值从弧度转换为度。默认情况下,
$asin返回值作为double类型。只要<expression>解析为 128 位十进制值,$asin也可以返回 128 位十进制值。有关表达式的更多信息,请参阅表达式运算符。
行为
如果参数解析为 null 或引用一个缺失的字段,$asin 返回 null。如果参数解析为 NaN,$asin 返回 NaN。如果参数解析为 [-1, 1] 范围之外的值,$asin 抛出错误。
示例 | 结果 | |||
|---|---|---|---|---|
{ $asin: NaN } | NaN | |||
{ $asin: null } | null | |||
或
| 抛出类似于以下格式输出的错误信息 |
示例
trigonometry 集合包含一个文档,该文档存储了一个直角三角形的三个边
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5") }
以下聚合操作使用 $asin 表达式来计算与 side_a 对应的角度,并使用 $addFields 管道阶段将其添加到输入文档中。
db.trigonometry.aggregate([ { $addFields : { "angle_a" : { $radiansToDegrees : { $asin : { $divide : [ "$side_a", "$hypotenuse" ] } } } } } ])
表达式 $radiansToDegrees 将由 $asin 返回的弧度值转换为等效的度数值。
该命令返回以下输出
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5"), "angle_a" : NumberDecimal("36.86989764584402129685561255909341") }
由于 side_a 和 hypotenuse 被存储为 128位十进制,因此 $asin 的输出是一个128位十进制数。
trigonometry 集合包含一个文档,该文档存储了一个直角三角形的三个边
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5") }
以下聚合操作使用 $asin 表达式计算与 side_a 相邻的角,并将其添加到输入文档中,使用 $addFields 管道阶段。
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") }
由于 side_a 和 hypotenuse 被存储为 128位十进制,因此 $asin 的输出是一个128位十进制数。