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

$tan(表达式操作符)

$tan

返回以弧度为单位来测量的某一值的正切值。

$tan 通过以下语法实现:

{ $tan: <expression> }

$tan 接受所有解析为数字的有效表达式。如果表达式返回以度为单位的值,请使用 $degreesToRadians 操作符将结果转换为弧度。

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

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

如果参数解析为 null 值或引用了缺失的字段,则 $tan 返回 null。如果参数解析为 NaN,则 $tan 返回 NaN。如果参数解析为负无穷大或正无穷大,$tan 将抛出错误。

例子
结果

{ $tan: NaN }

NaN

{ $tan: null }

null

{ $tan : Infinity}

or

{ $tan : -Infinity }

抛出一条类似以下格式化输出的错误消息:

"errmsg" : "Failed to optimize pipeline :: caused by :: cannot apply $tan to -inf, value must in (-inf,inf)"

The trigonometry collection 包含一个文档,其中存储了直角三角形的一条边和一个角:

{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : Decimal128("53.13010235415597870314438744090659"), "side_a" : Decimal128("3")}

下面的聚合操作使用 $tan 表达式计算与 angle_a 相对的边,并使用 $addFields 管道阶段将其添加到输入文档中。

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

$degreesToRadians 表达式将 angle_a 的度值转换为弧度的等效值。

该命令返回以下输出:

{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : Decimal128("53.13010235415597870314438744090659"), "side_a" : Decimal128("3") "side_b" : Decimal128("4.000000000000000000000000000000000")}

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

给本页内容打分

在此页面上