In MongoDB, the $acosh
aggregation pipeline operator returns the hyperbolic arccosine (inverse hyperbolic cosine) of a value, measured in radians.
$acosh
accepts any valid expression that resolves to a number between 1
and +Infinity
.
The $acosh
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 $acosh
operator to return the hyperbolic arccosine of the data
field:
db.test.aggregate(
[
{ $project: {
_id: 0,
hyperbolicArccosine: { $acosh: "$data" }
}
}
]
)
Result:
{ "hyperbolicArccosine" : 1.762747174039086 }
By default, the $acosh
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.
Convert to Degrees
As mentioned, $acosh
returns its result in radians. You can use the $radiansToDegrees
operator if you want the result in degrees.
Example:
db.test.aggregate(
[
{ $project: {
_id: 0,
radians: { $acosh: "$data" },
degrees: { $radiansToDegrees: { $acosh: "$data" } }
}
}
]
)
Result:
{ "radians" : 1.762747174039086, "degrees" : 100.99797342105244 }
In this example, the first field presents the result in radians, and the second field presents it in degrees.
Out of Range Values
Providing an out of range value to $acosh
will result in an error.
Suppose we add the following document to the collection:
{ "_id" : 2, "data" : 0 }
Now let’s run $acosh
against the data
field:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
radians: { $acosh: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "cannot apply $acosh to 0, value must in [1,inf]", "code" : 50989, "codeName" : "Location50989" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
As the error message indicates, the value must be between 1
and +Infinity
.
Null Values
Null values return null
when using the $acosh
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $acos
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $acosh: "$data" }
}
}
]
)
Result:
{ "result" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $acosh
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $acosh: 1 * "string" }
}
}
]
)
Result:
{ "result" : 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
, the $acosh
operator returns Infinity
.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $acosh: Infinity }
}
}
]
)
Result:
{ "result" : Infinity }
However, -Infinity
will return an error.
Non-Existent Fields
If the $acosh
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $acosh: "$wrongField" }
}
}
]
)
Result:
{ "result" : null }