对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

$atan2(表达式操作符)

$atan2

返回y / x的反正切,其中yx分别是传递给表达式的第一个值和第二个值。

$atan2 通过以下语法实现:

{ $atan2: [ <expression 1>, <expression 2> ] }

$atan2接受解析为数字的任何有效表达式。

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

默认下,$atan2 128$atan2返回double<expression> 128形式的值。只要 解析为 位十进制值, 也可以返回 位十进制值。

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

如果赋予$atan2的任一参数为null ,则表达式返回null 。 如果任一参数为NaN ,则表达式返回NaN 。 如果一个参数为null ,另一个为NaN ,则表达式返回null

例子
结果

{ $atan2: [ NaN, <value> ] }

NaN

{ $atan2: [ <value>, NaN ] }

NaN

{ $atan2: [ null, <value> ] }

null

{ $atan2: [ <value>, null ] }

null

{ $atan2: [ NaN, null ] }

null

{ $atan2: [ null, NaN ] }

null

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

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

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

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$radiansToDegrees : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
}
])

$radiansToDegrees表达式将$atan2 返回的弧度值转换为等效的度数值。

该命令返回以下输出:

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

由于side_bside_a 128存储为$atan2 位十进制数,因此 的输出为128 位十进制数。

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

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

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

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
])

该命令返回以下输出:

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

由于side_bside_a 128存储为$atan2 位十进制数,因此 的输出为128 位十进制数。

给本页内容打分

在此页面上