Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$asin(聚合)

在此页面上

  • 行为
  • 例子
$asin

返回值的反正弦。

$asin 通过以下语法实现:

{ $asin: <expression> }

$asin接受解析为-11之间数字的任何有效表达式,例如-1 <= value <= 1

$asin 以弧度为单位返回值。使用 $radiansToDegrees 操作符,将输出值从弧度转换为度数。

默认情况下, $asindouble形式返回值。 只要<expression>解析为128位十进制值, $asin也可以返回128位十进制值。

有关表达式的更多信息,请参阅表达式

如果参数解析为 null 值或引用了缺失的字段,则 $asin 返回 null。如果参数解析为 NaN,则 $asin 返回 NaN。如果参数值超出了 [-1, 1] 范围,$asin 会抛出错误。

例子
结果
{ $asin: NaN }
NaN
{ $asin: null }
null

{ $asin : Infinity}

或是

{ $asin : -Infinity }

抛出一条类似以下格式化输出的错误消息:

"errmsg" :
"Failed to optimize pipeline :: caused by :: cannot
apply $asin to -inf, value must in [-1,1]"

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_ahypotenuse存储为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_ahypotenuse存储为128位十进制数,因此$asin的输出是128位十进制数。

后退

$arrayToObject

来年

$asinh

在此页面上