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

$tanh(表达式操作符)

$tanh

以弧度为单位返回数值的双曲正切值。

$tanh 通过以下语法实现:

{ $tanh: <expression> }

$tanh takes any valid expression that resolves to a number, measured in radians. If the expression returns a value in degrees, use the $degreesToRadians operator to convert the value to radians.

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

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

If the input argument resolves to a value of null or refers to a field that is missing, $tanh returns null. If the argument resolves to NaN, $tanh returns NaN. If the argument resolves to negative or positive Infinity, $tanh returns -1 or 1 respectively.

例子
结果

{ $tanh: NaN }

NaN

{ $tanh: null }

null

{ $tanh: -Infinity }

-1

{ $tanh: Infinity }

1

trigonometry以下collection包含一个文档,其中存储了一个以度数为单位的angle 值:

db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d45" ),
"angle" : Decimal128( "53.1301023541559787031443874490659" )
}
)

The following aggregation operation uses the $tanh expression to calculate the hyperbolic tangent of angle and adds it to the input document using the $addFields pipeline stage:

db.trigonometry.aggregate( [
{
$addFields : {
"tanh_output" : { $tanh : { $degreesToRadians : "$angle" } }
}
}
] )

$degreesToRadians表达式将以度为单位的angle转换为弧度。

示例输出:

{
"_id" : ObjectId("5c50782193f833234ba90d45"),
"angle" : Decimal128("53.1301023541559787031443874490659"),
"tanh_output" : Decimal128("0.7293303448445332820512777329448416")
}

Because angle is stored as a 128-bit decimal, the $tanh output is also a 128-bit decimal.

给本页内容打分

在此页面上