Docs HomeMongoDB Manual

$cosh (aggregation)

On this page本页内容

$cosh

New in version 4.2. 4.2版新增。

Returns the hyperbolic cosine of a value that is measured in radians.返回以弧度为单位测量的值的双曲余弦。

$cosh has the following syntax:具有以下语法:

{ $cosh: <expression> }

$cosh 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.如果表达式返回以度为单位的值,请使用$degreesToRadians运算符将该值转换为弧度。

By default $cosh returns values as a double. 默认情况下,$coshdouble返回值。$cosh can also return values as a 128-bit decimal if the <expression> resolves to a 128-bit decimal value.如果<expression>解析为128位十进制值,则也可以以128位十进制形式返回值。

For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

null, NaN, and +/- Infinity

If the input argument resolves to a value of null or refers to a field that is missing, $cosh returns null. 如果输入参数解析为null值或引用了缺失的字段,$cosh将返回nullIf the argument resolves to NaN, $cosh returns NaN. 如果参数解析为NaN$cosh将返回NaNIf the argument resolves to negative or positive Infinity, $cosh returns positive Infinity.如果参数解析为负Infinity或正Infinity$cosh将返回正无穷大。

Example示例Results结果
{ $cosh: NaN }NaN
{ $cosh: null }null
{ $cosh: -Infinity }Infinity
{ $cosh: Infinity }Infinity

Example实例

The following trigonometry collection contains a document that stores an angle value measured in degrees:以下trigonometry集合包含一个文档,该文档存储以度为单位测量的angle值:

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

The following aggregation operation uses the $cosh expression to calculate the hyperbolic cosine of angle and adds it to the input document using the $addFields pipeline stage:以下聚合操作使用$cosh表达式计算angle的双曲余弦,并使用$addFields管道阶段将其添加到输入文档:

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

The $degreesToRadians expression converts the angle in degrees to radians.$degreesToRadians表达式将angle(以度为单位)转换为弧度。

Example output:示例输出:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle" : NumberDecimal("53.1301023541559787031443874490659"),
"cosh_output" : NumberDecimal("1.461642741099671277595921778079396")
}

Because angle is stored as a 128-bit decimal, the $cosh output is also a 128-bit decimal.因为angle存储为128位十进制,所以$cosh输出也是128位十进制。

The following trigonometry collection contains a document that stores an angle value measured in radians:以下trigonometry集合包含一个文档,该文档存储以弧度为单位测量的角度值:

db.trigonometry.insertOne(
{
"_id" : ObjectId( "5c50782193f833234ba90d15" ),
"angle" : NumberDecimal( "1.6301023541559787031443874490659" )
}
)

The following aggregation operation uses the $cosh expression to calculate the hyperbolic cosine of angle and adds it to the input document using the $addFields pipeline stage:以下聚合操作使用$cosh表达式计算angle的双曲余弦,并使用$addFields管道阶段将其添加到输入文档:

db.trigonometry.aggregate( [
{
$addFields : {
"cosh_output" : { $cosh : "$angle" }
}
}
] )

Example output:输出示例:

{
"_id" : ObjectId("5c50782193f833234ba90d15"),
"angle" : NumberDecimal("1.6301023541559787031443874490659"),
"cosh_output" : NumberDecimal("2.650153334504361016712328539738000")
}

Because angle is stored as a 128-bit decimal, the $cosh output is also a 128-bit decimal.因为angle存储为128位十进制,所以$cosh输出也是128位十进制。