$atanh is a MongoDB aggregation expression that returns the inverse hyperbolic tangent (arc hyperbolic tangent) of a number—the value y such that tanh(y) equals that input. The argument must be strictly between-1 and 1 (not including ±1).
Remember
$atanh = Math.atanh for aggregation
{ $atanh: <expression> }
input → must be in (-1, 1) // not ±1
output → real number (atanh is odd: atanh(-x) = -atanh(x))
check → atanh(0) = 0
null / missing → null
outside (-1, 1) → pipeline error
Ensure the argument is strictly between -1 and 1 (not equal to ±1).
Pass that expression to $atanh.
Optionally wrap with $round for a shorter display value.
Piece
Required?
Role
$atanh
Yes
Inverse hyperbolic tangent
Expression
Yes
Number in (-1, 1), field, or nested math
$round
No
Trim floating-point noise for reports
Pseudocode
Pseudocode
for each document:
x = evaluate(expression)
if x is null or missing:
result = null
else if x is NaN:
result = NaN
else if x <= -1 or x >= 1:
error // aggregation fails
else:
result = atanh(x)
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing inverse-hyperbolic-tangent expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $atanh: <expression> }
Domain
-1 < x < 1 (exclusive)
Odd property
atanh(-x) = -atanh(x)
Zero check
atanh(0) = 0
Round-trip
$tanh({ $atanh: x }) ≈ x
Sample check
atanh(0.5) ≈ 0.5493
Try it
Live Preview
Watch $atanh turn a value into its inverse hyperbolic tangent. Stay inside (-1, 1).
Simulates $atanh (same idea as the mongosh examples below).
Result
x = 0.5
$atanh(x) → 0.549306
{ atanh_value: 0.549306 }
Trace
Worked Walkthrough — atanh(0.5)
Start with a single number 0.5. We want the value y where tanh(y) = 0.5.
Calculation
Document:
{ value: 0.5 }
Goal: find y such that tanh(y) = 0.5
$atanh(0.5) ≈ 0.5493061443340548
Check with hyperbolic tangent:
tanh(0.5493061443340548) ≈ 0.5 ✓
Also remember:
atanh(0) = 0
atanh(-0.5) ≈ -0.5493 // odd function
atanh(±1) → invalid (outside domain)
Code
MongoDB Examples
We use three small numbers inside the domain: 0, 0.5, and -0.5. First we store them, then we compute $atanh, then we round for cleaner output. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine three readings inside (-1, 1): zero, one positive, and one negative. We save each as its own document in a collection named hyperbolic.
Same stored documents. Still using values 0, 0.5, and -0.5 from Example 1.
Same $atanh first. Full precision is computed, then trimmed.
Round to 4 decimals.$round keeps reports readable (0.5493 instead of a long float).
Easy check. Zero stays 0; positive and negative results stay mirrors.
Edge Cases & Pitfalls
Check these before using $atanh in production pipelines.
domain
Stay inside (-1, 1)
±1 and values outside the open interval make the pipeline fail. Validate or clamp before calling $atanh.
name
Do not confuse with $atan
$atan is inverse tangent (trig). $atanh is hyperbolic and requires |x| < 1.
null
Null and missing become null
Missing fields return null, not an error. Handle them before math if nulls are invalid in your data.
ends
Results grow near ±1
As x approaches the ends of the domain, |atanh(x)| grows quickly. That is expected math, not a MongoDB bug.
Analysis
Performance Notes
Approach
Cost
Best for
$atanh alone
Per-document math
Simple inverse hyperbolic tangent fields
$atanh + $round
Still O(docs)
Readable report columns
$tanh round-trip check
Two math ops
Validating transforms in tests
Trig and hyperbolic expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need the transform.
Remember
Key Takeaways
Role: inverse hyperbolic tangent of a number in (-1, 1).
Domain:-1 < x < 1 — ±1 is invalid.
Pair: reverse of $tanh; not the same as $atan.
Smoke test:atanh(0) = 0.
One line:$atanh answers “which value has this hyperbolic tangent?”—for inputs strictly between -1 and 1.
Frequently Asked Questions
$atanh returns the inverse hyperbolic tangent (arc hyperbolic tangent) of a number—the value y such that tanh(y) equals that number. Use it inside aggregation expression stages such as $project and $addFields.
The argument must resolve to a number strictly between -1 and 1 (not including ±1). Values outside that open interval make the aggregation fail.
atanh(0) equals 0. That makes zero a handy smoke-test when checking pipeline output.
$atanh returns null when the input is null or the field is missing. If the argument is NaN, $atanh returns NaN.
$atan is inverse tangent (trig, any real input). $atanh is inverse hyperbolic tangent (domain (-1, 1)). Same naming style, different math.
Yes for the math. Think of $atanh as MongoDB’s aggregation version of Math.atanh—run it in the pipeline, not as a standalone find() filter.