文档菜单
文档首页
/
MongoDB 手册
/ / /

$acos (聚合)

本页内容

  • 行为
  • 示例
$acos

返回值的反余弦(反余弦)。

$acos 的语法如下

{ $acos: <expression> }

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

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

默认情况下,$acos 返回的值是 double 类型。只要 <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_bhypotenuse128位十进制数 存储,所以 $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_bhypotenuse128位十进制数 存储,所以 $acos 的输出是一个 128 位十进制数。

返回

$accumulator

本页内容