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

$atan(表达式操作符)

$atan

返回某个值的反正切值。

$atan 通过以下语法实现:

{ $atan: <expression> }

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

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

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

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

如果参数解析为null值或引用了缺失的字段,则$atan返回null 。 如果参数解析为NaN ,则$tan返回NaN 。

例子
结果

{ $atan: NaN }

NaN

{ $atan: null }

null

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

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

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

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

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

该命令返回以下输出:

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

由于 side_b 和 side_a 存储为 128 位小数,因此$atan 的输出为 128 位小数。

给本页内容打分

在此页面上