MongoDB $atanh Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Hyperbolic Math

What You’ll Learn

The $atanh operator returns the inverse hyperbolic tangent of a number in MongoDB aggregation pipelines. It is the reverse of $tanh and is commonly used for data transforms and statistical modeling.

01

Inverse atanh

Reverse of hyperbolic tangent.

02

Syntax

One expression inside $atanh.

03

Input Range

Values between -1 and 1.

04

$project Stage

Add computed fields easily.

05

Use Cases

Stats, ML, transforms.

06

Edge Case

atanh(0) = 0

Definition and Usage

In MongoDB’s aggregation framework, the $atanh operator computes the inverse hyperbolic tangent of a numeric expression. If tanh(y) = x, then atanh(x) = y. For example, $atanh(0.5) returns approximately 0.549, and $atanh(0) returns 0.

💡
Beginner Tip

Do not confuse $atanh with $atan. $atanh is hyperbolic math (like Math.atanh()), while $atan is regular trigonometry. They have different input rules and meanings.

📝 Syntax

The $atanh operator takes one numeric expression:

mongosh
{ $atanh: <expression> }

Syntax Rules

  • $atanh — returns the inverse hyperbolic tangent of the expression.
  • <expression> — must evaluate to a number strictly between -1 and 1.
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
  • Values of exactly 1, -1, or outside the range return null or NaN.

⚠️ Valid Input Range: -1 to 1 (Exclusive at Boundaries)

$atanh only accepts values between -1 and 1. The endpoints themselves are undefined.

$atanh: 0.50.549 (valid)
$atanh: 00 (valid)
$atanh: 1null (undefined!)
$atanh: 1.2null (out of range!)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (hyperbolic)
Syntax{ $atanh: <expression> }
Input rangeGreater than -1 and less than 1
Inverse of$tanh
Common stages$project, $addFields, $set
Example
{
  $atanh: 0.5
}

≈ 0.549

Negative
{
  $atanh: -0.5
}

≈ -0.549

Zero
{
  $atanh: 0
}

Returns 0

Null input
{
  $atanh: null
}

Returns null

Examples Gallery

Compute inverse hyperbolic tangent on sample values between -1 and 1, including positive, negative, and zero inputs.

📚 Basic Computation

Use a values collection and compute $atanh for each numeric field with $project.

Sample Input Documents

Suppose you have a values collection with a numeric x field:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "x": 0.5 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "x": 0.7 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "x": 0.9 }
]

Example 1 — Basic $atanh on a Field

Compute the inverse hyperbolic tangent for each value:

mongosh
db.values.aggregate([
  {
    $project: {
      x: 1,
      inverseTanh: { $atanh: "$x" }
    }
  }
])

How It Works

  • Document 1: atanh(0.5)0.549.
  • Document 2: atanh(0.7)0.867.
  • Document 3: atanh(0.9)1.472 (values near 1 grow rapidly).

📈 Practical Patterns

Handle negative values, round results, and guard against out-of-range inputs.

Example 2 — Negative Values

$atanh is an odd function: atanh(-x) = -atanh(x):

mongosh
db.values.aggregate([
  {
    $project: {
      x: 1,
      inverseTanh: { $atanh: "$x" },
      inverseTanhRounded: {
        $round: [ { $atanh: "$x" }, 3 ]
      }
    }
  }
])

// Sample values: -0.5, 0, 0.5
// atanh(-0.5) ≈ -0.549
// atanh(0)    =  0
// atanh(0.5)  ≈  0.549

How It Works

Negative inputs within (-1, 1) produce negative outputs. Use $round for cleaner display values.

Example 3 — Fisher z-Transform (Statistical Use)

The inverse hyperbolic tangent is used in statistics to transform correlation coefficients:

mongosh
db.correlations.aggregate([
  {
    $project: {
      pair: 1,
      correlation: 1,
      fisherZ: { $atanh: "$correlation" }
    }
  },
  {
    $group: {
      _id: null,
      avgFisherZ: { $avg: "$fisherZ" }
    }
  }
])

How It Works

Correlation values naturally fall between -1 and 1, making them ideal input for $atanh. The Fisher z-transform stabilizes variance before averaging.

Example 4 — Guard Out-of-Range Values

Skip or flag values outside the valid domain before applying $atanh:

mongosh
db.values.aggregate([
  {
    $project: {
      x: 1,
      inverseTanh: {
        $cond: {
          if: {
            $and: [
              { $gt: ["$x", -1] },
              { $lt: ["$x", 1] }
            ]
          },
          then: { $atanh: "$x" },
          else: null
        }
      }
    }
  }
])

// x: 0.5  → inverseTanh: 0.549 (valid)
// x: 1    → inverseTanh: null   (guarded)
// x: 1.2  → inverseTanh: null   (guarded)

How It Works

Use $cond with $gt and $lt to check the domain before calling $atanh. This prevents unexpected null or NaN results in production pipelines.

🚀 Use Cases

  • Mathematical computations — advanced analyses involving hyperbolic functions in aggregation pipelines.
  • Statistical analysis — Fisher z-transform on correlation coefficients or normalized data.
  • Machine learning — preprocessing features bounded between -1 and 1 before modeling.
  • Data normalization — reverse a $tanh scaling step to recover original magnitudes.

🧠 How $atanh Works

1

MongoDB reads the value

The pipeline evaluates the input — a field like "$x" or a numeric expression between -1 and 1.

Input
2

$atanh computes the inverse

MongoDB applies the inverse hyperbolic tangent function. Values outside (-1, 1) return null or NaN.

Transform
3

The result is stored in the pipeline

The computed value is written to the field you define in $project or $addFields.

Output
=

Transformed numeric data

Use the result for grouping, averaging, or further pipeline stages.

Conclusion

The $atanh operator is a useful hyperbolic math tool in MongoDB aggregation pipelines. It reverses $tanh scaling, supports the Fisher z-transform in statistics, and handles negative values naturally within its valid domain.

For beginners, remember: input must be between -1 and 1 (not including the endpoints), atanh(0) = 0, and do not confuse $atanh with the trigonometric $atan operator.

💡 Best Practices

✅ Do

  • Validate that input values are between -1 and 1 before applying
  • Remember atanh(0) = 0 when checking results
  • Use $round for readable output values
  • Pair with $tanh to verify round-trip calculations
  • Guard with $cond in production pipelines

❌ Don’t

  • Confuse $atanh with $atan (different functions)
  • Pass values of exactly 1 or -1 without handling undefined results
  • Pass correlation values of ±1 without a guard
  • Use $atanh as a query filter outside expressions
  • Forget that values near ±1 produce very large outputs

Key Takeaways

Knowledge Unlocked

Five things to remember about $atanh

Use these points when working with hyperbolic math in MongoDB.

5
Core concepts
📝 02

Simple Syntax

{ $atanh: expr }

Syntax
📏 03

Input Range

Between -1 and 1.

Domain
🔢 04

Odd Function

atanh(-x) = -atanh(x).

Property
05

Not $atan

Hyperbolic, not trig.

Important

❓ Frequently Asked Questions

$atanh returns the inverse hyperbolic tangent (arc hyperbolic tangent) of a number. It is the reverse of $tanh and is used in aggregation expression stages like $project and $addFields.
The syntax is { $atanh: <expression> }. The expression can be a field reference like "$x", a literal number, or another numeric expression.
$atanh only works for values strictly between -1 and 1. Input of exactly 1, -1, or anything outside that range returns null or NaN.
atanh(0) equals 0. This is a useful edge case when validating pipeline results.
No. $atanh is inverse hyperbolic tangent (hyperbolic math). $atan is inverse tangent (trigonometry). They are different functions with different input rules.

Continue the Operator Series

Move on to $binarySize or review $asinh for another hyperbolic inverse operator.

Next: $binarySize →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful