$atan返回某个值的反正切值。
$atan通过以下语法实现:{ $atan: <expression> } $atan接受解析为数字的任何有效表达式。$atan以弧度为单位返回值。使用$radiansToDegrees操作符将输出值从弧度转换为度数。默认下,
$atan128$atan返回double<expression>128形式的值。只要 解析为 位十进制值, 也可以返回 位十进制值。有关表达式的更多信息,请参阅表达式。
行为
null 和 NaN
如果参数解析为null 的值或引用了缺失的字段,$atan 将返回null 。如果参数解析为NaN ,$tan 将返回NaN 。
例子 | 结果 |
|---|---|
|
|
|
|
例子
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") }
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" : { $atan : { $divide : [ "$side_b", "$side_a" ] } } } } ])
该命令返回以下输出:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : Decimal128("3"), "side_b" : Decimal128("4"), "hypotenuse" : Decimal128("5"), "angle_a" : Decimal128("0.9272952180016122324285124629224287") }