$atan (聚合)
$atan
返回值的反正切(反正切)。
$atan
有以下语法{ $atan: <expression> } $atan
返回弧度值。使用$radiansToDegrees
运算符将输出值从弧度转换为度。默认情况下,
$atan
返回值为double
类型。只要<expression>
解析为 128 位十进制值,$atan
也可以返回 128 位十进制 值。有关表达式更多信息,请参阅 表达式运算符。
行为
null
和 NaN
如果参数解析为 null
或引用一个缺失的字段,$atan
返回 null
。如果参数解析为 NaN
,则 $tan
返回 NaN
。
示例 | 结果 |
---|---|
{ $atan: NaN } | NaN |
{ $atan: null } | null |
示例
三角学集合包含一个文档,该文档存储了直角三角形的三个边
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("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" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5"), "angle_a" : NumberDecimal("53.13010235415597870314438744090658") }
由于side_b
和side_a
存储为128位十进制数,因此$atan
的输出是128位十进制数。
三角学集合包含一个文档,该文档存储了直角三角形的三个边
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5") }
以下聚合操作使用$atan
表达式计算与side_a
相邻的角度,并使用$addFields
管道阶段将其添加到输入文档中。
db.trigonometry.aggregate([ { $addFields : { "angle_a" : { $atan : { $divide : [ "$side_b", "$side_a" ] } } } } ])
该命令返回以下输出
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : NumberDecimal("3"), "side_b" : NumberDecimal("4"), "hypotenuse" : NumberDecimal("5"), "angle_a" : NumberDecimal("0.9272952180016122324285124629224287") }
由于side_b
和side_a
存储为128位十进制数,因此$atan
的输出是128位十进制数。