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

$tan(表达式操作符)

$tan

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

$tan 通过以下语法实现:

{ $tan: <expression> }

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

By default $tan returns values as a double. $tan can also return values as a 128-bit decimal as long as the <expression> resolves to a 128-bit decimal value.

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

如果参数解析为 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 位十进制数。

给本页内容打分

在此页面上