In MongoDB, the $cosh
aggregation pipeline operator returns the hyperbolic cosine of a value that is measured in radians.
$cosh
accepts any valid expression that resolves to a number.
The $cosh
operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 3 }
We can use the $cosh
operator to return the cosine of the data
field:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: "$data" }
}
}
]
)
Result:
{ "hyperbolicCosine" : 10.067661995777765 }
By default, the $cosh
operator returns values as a double
, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.
When the Expression is in Degrees
As mentioned, $cosh
accepts its expression in radians. You can use the $degreesToRadians
operator to convert any values from degrees to radians.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $degreesToRadians: { $cosh: "$data" } }
}
}
]
)
Result:
{ "hyperbolicCosine" : 0.17571384980422547 }
Null Values
Null values return null
when using the $cosh
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $cosh
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: "$data" }
}
}
]
)
Result:
{ "hyperbolicCosine" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $cosh
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: 1 * "string" }
}
}
]
)
Result:
{ "hyperbolicCosine" : NaN }
In this case I tried to multiple a number by a string, which resulted in NaN
being returned.
Infinity
If the argument resolves to Infinity
or -Infinity
, the $cosh
operator returns Infinity
.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : Infinity }
Let’s run $cosh
against the data field:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: "$data" }
}
}
]
)
Result:
{ "hyperbolicCosine" : Infinity }
Non-Existent Fields
If the $cosh
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: "$name" }
}
}
]
)
Result:
{ "hyperbolicCosine" : null }
128-bit Decimal
As mentioned, if the expression provided to $cosh
is 128-bit decimal, then the result is returned in 128-bit decimal.
Suppose we add the following document to the collection:
{ "_id" : 5, "data" : NumberDecimal("1.1301023541559787031443874490659") }
Here’s what happens when we run that through the $cosh
operator:
db.test.aggregate(
[
{ $match: { _id: 5 } },
{ $project: {
_id: 0,
hyperbolicCosine: { $cosh: "$data" }
}
}
]
)
Result:
{ "hyperbolicCosine" : NumberDecimal("1.709486781983575502518713909095045") }
The output is 128-bit decimal.