In MongoDB, the $sinh
aggregation pipeline operator returns the hyperbolic sine of a value that is measured in radians.
$sinh
accepts any valid expression that resolves to a number.
The $sinh
operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 2 }
We can use the $sinh
operator to return the hyperbolic sine of the data
field:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicSine: { $sinh: "$data" }
}
}
]
)
Result:
{ "hyperbolicSine" : 3.6268604078470186 }
Convert to Radians
As mentioned, $sinh
returns the sine of a value that is measured in radians. If the value is in degrees, you can use the $degreesToRadians
operator to convert it to radians.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicSine: { $degreesToRadians: { $sinh: "$data" } }
}
}
]
)
Result:
{ "hyperbolicSine" : 0.06330065562715485 }
128-Bit Decimal Values
By default, the $sinh
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.
Suppose we add the following document to our collection:
{ "_id" : 2, "data" : NumberDecimal("2.1301023541559787031443874490659") }
Let’s run the the $sinh
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
result: { $sinh: "$data" }
}
}
]
)
Result:
{ "result" : NumberDecimal("4.148451510563232456419254731162938") }
The output is 128-bit decimal.
Null Values
Null values return null
when using the $sinh
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $sinh
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $sinh: "$data" }
}
}
]
)
Result:
{ "result" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $sinh
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $sinh: 1 * "$data" }
}
}
]
)
Result:
{ "result" : NaN }
Non-Existent Fields
If the $sinh
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $sinh: "$name" }
}
}
]
)
Result:
{ "result" : null }
Infinity
Providing Infinity
returns Infinity
, and providing -Infinity
returns -Infinity
.
Suppose we add the following documents to the collection:
{ "_id" : 4, "data" : Infinity } { "_id" : 5, "data" : -Infinity }
Let’s apply $sinh
to these documents:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 4, 5 ] } } },
{ $project: {
hyperbolicSine: { $sinh: "$data" }
}
}
]
)
Result:
{ "_id" : 4, "hyperbolicSine" : Infinity } { "_id" : 5, "hyperbolicSine" : -Infinity }