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

$acos(聚合)

在此页面上

  • 行为
  • 例子
$acos

返回某个值的反余弦值。

$acos 通过以下语法实现:

{ $acos: <expression> }

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

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

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

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

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

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

{ $acos : Infinity}

或是

{ $acos : -Infinity }

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

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

trigonometry 集合包含一个文档,该文档存储直角三角形的三条边:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5")
}

以下聚合操作使用$acos表达式计算与side_a相邻的角度,并使用$addFields管道阶段将其添加到输入文档。

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$radiansToDegrees : {
$acos : {
$divide : [ "$side_b", "$hypotenuse" ]
}
}
}
}
}
])

$radiansToDegrees表达式将$acos返回的弧度值转换为以度为单位的等效值。

该命令返回以下输出:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("36.86989764584402129685561255909341")
}

由于side_bhypotenuse存储为128位十进制数,因此$acos的输出是128位十进制数。

trigonometry 集合包含一个文档,该文档存储直角三角形的三条边:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5")
}

以下聚合操作使用$acos表达式计算与side_a相邻的角度,并使用$addFields管道阶段将其添加到输入文档。

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$acos : {
$divide : [ "$side_b", "$hypotenuse" ]
}
}
}
}
])

该命令返回以下输出:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("0.6435011087932843868028092287173226")
}

由于side_bhypotenuse存储为128位十进制数,因此$acos的输出是128位十进制数。

后退

$accumulator

来年

$acosh

在此页面上