In MongoDB, the $atanh aggregation pipeline operator returns the hyperbolic arctangent (inverse hyperbolic tangent) of a value.
The return value is in radians.
$atanh accepts any valid expression that resolves to a number between -1 and 1.
The $atanh operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test with the following document:
{ "_id" : 1, "data" : 0.5 }
We can use the $atanh operator to return the hyperbolic arctangent of the data field:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
hyperbolicArctangent: { $atanh: "$data" }
}
}
]
)
Result:
{ "hyperbolicArctangent" : 0.5493061443340549 }
Convert to Degrees
As mentioned, $atanh returns its result in radians. You can use the $radiansToDegrees operator if you want the result in degrees.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
radians: { $atanh: "$data" },
degrees: { $radiansToDegrees: { $atanh: "$data" } }
}
}
]
)
Result:
{ "radians" : 0.5493061443340549, "degrees" : 31.472923730945382 }
In this example, the first field presents the result in radians, and the second field presents it in degrees.
128-Bit Decimal Values
By default, the $atanh 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("0.1301023541559787031443874490659") }
Let’s run the the $atanh operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
result: { $atanh: "$data" }
}
}
]
)
Result:
{ "result" : NumberDecimal("0.1308439651155512740523796431117568") }
The output is 128-bit decimal.
Out of Range Values
The $atanh operator accepts any valid expression that resolves to a number between -1 and 1. Values outside of that range will cause an error.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : 2 }
Let’s run the the $atanh operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $atanh: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $atanh to 2, value must in [-1,1]",
"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
Null Values
Null values return null when using the $atanh operator.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : null }
Let’s run the the $atanh operator against that document:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
result: { $atanh: "$data" }
}
}
]
)
Result:
{ "result" : null }
We can see that the result is null.
NaN Values
If the argument resolves to NaN, $atanh returns NaN.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $atanh: 1 * "$data" }
}
}
]
)
Result:
{ "result" : NaN }
Non-Existent Fields
If the $atanh operator is applied against a field that doesn’t exist, null is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $atanh: "$name" }
}
}
]
)
Result:
{ "result" : null }
Infinity
Providing Infinity or -Infinity will return an out of range error (like we saw earlier).
Suppose we add the following document to the collection:
{ "_id" : 5, "data" : Infinity }
Let’s run $atanh again:
db.test.aggregate(
[
{ $match: { _id: 5 } },
{ $project: {
_id: 0,
result: { $atanh: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $atanh to inf, value must in [-1,1]",
"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