MongoDB $cosh Operator

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

What You’ll Learn

The $cosh operator computes the hyperbolic cosine of a number in MongoDB aggregation pipelines. Use it for exponential growth models, catenary curves, and advanced math transformations on numeric data.

01

Hyperbolic Cosine

Exponential-style math.

02

Syntax

One expression inside $cosh.

03

Output ≥ 1

Always at least 1.

04

Even Function

cosh(-x) = cosh(x).

05

Use Cases

Growth, physics, stats.

06

vs $cos

Hyperbolic vs circular.

Definition and Usage

In MongoDB’s aggregation framework, the $cosh operator returns the hyperbolic cosine of a numeric expression. The formula is cosh(x) = (ex + e-x) / 2. For example, cosh(0) = 1 and cosh(1) ≈ 1.543. Unlike circular cosine, the result is always greater than or equal to 1.

💡
Beginner Tip

Think of $cosh as MongoDB’s version of JavaScript’s Math.cosh(). Use it inside aggregation expression stages like $project or $addFields, not as a query filter.

📝 Syntax

The $cosh operator takes one numeric expression:

mongosh
{ $cosh: <expression> }

Syntax Rules

  • $cosh — returns the hyperbolic cosine of the expression.
  • <expression> — a field reference, literal, or nested numeric expression.
  • Output is always ≥ 1 (minimum value is 1 at input 0).
  • $cosh is an even function: cosh(-x) = cosh(x).
  • Use inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.

⚠️ Output Is Always ≥ 1

$cosh(0)1 (minimum value)
$cosh(1)≈ 1.543
$cosh(2)≈ 3.762 (grows quickly)
Unlike $cos, hyperbolic cosine never goes below 1

💡 $cosh vs $cos vs $acosh

$cosh — hyperbolic cosine; output ≥ 1; grows exponentially
$cos — circular cosine; output -1 to 1; periodic waves
$acosh — inverse of $cosh; input must be ≥ 1

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (hyperbolic)
Syntax{ $cosh: <expression> }
InputAny real number
Output range≥ 1 (unbounded above)
Common stages$project, $addFields, $set
Zero
{
  $cosh: 0
}

Returns 1

Field
{
  $cosh: "$x"
}

From document field

Example
{
  $cosh: 1
}

≈ 1.543

Null input
{
  $cosh: null
}

Returns null

Examples Gallery

Compute hyperbolic cosines from stored values, verify the even-function property, and explore growth patterns with $cosh.

📚 Basic Computation

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

Sample Input Documents

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

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "x": 0 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "x": 1 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "x": 2 }
]

Example 1 — Basic $cosh on a Field

Compute the hyperbolic cosine of each stored value:

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

How It Works

  • x: 0cosh(0) = 1 (minimum value).
  • x: 1cosh(1) ≈ 1.543.
  • x: 2cosh(2) ≈ 3.762 (grows faster than linear).

📈 Practical Patterns

Verify the even-function property, model growth, and test boundary values.

Example 2 — Even Function: cosh(-x) = cosh(x)

Hyperbolic cosine returns the same result for positive and negative inputs:

mongosh
db.values.aggregate([
  {
    $project: {
      x: 1,
      coshPositive: { $cosh: "$x" },
      coshNegative: { $cosh: { $multiply: [ "$x", -1 ] } }
    }
  }
])

// x: 1  → coshPositive ≈ 1.543, coshNegative ≈ 1.543
// x: -2 → both fields return ≈ 3.762

How It Works

This symmetry is useful when your data may contain signed values but the hyperbolic result should be the same magnitude regardless of sign.

Example 3 — Exponential Growth Modeling

Hyperbolic cosine appears in compound growth and catenary (hanging cable) formulas:

mongosh
db.metrics.aggregate([
  {
    $project: {
      time: 1,
      growthFactor: { $cosh: "$time" },
      scaledGrowth: {
        $multiply: [
          { $cosh: "$time" },
          "$baseRate"
        ]
      }
    }
  }
])

How It Works

As time increases, $cosh(time) grows exponentially. Pair with other operators to build physics, finance, or statistics models inside the pipeline.

Example 4 — $cosh with Literal Values

Common values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      coshZero:  { $cosh: 0 },    // 1
      coshOne:   { $cosh: 1 },    // ≈ 1.543
      coshTwo:   { $cosh: 2 },    // ≈ 3.762
      coshNegOne:{ $cosh: -1 }   // ≈ 1.543 (same as cosh(1))
    }
  }
])

How It Works

$cosh(0) = 1 is the minimum. $cosh(-1) equals $cosh(1), confirming the even-function property.

🚀 Use Cases

  • Exponential growth analysis — model rapid increases in metrics, populations, or investments.
  • Physics and engineering — catenary curves, heat diffusion, and wave equations involving hyperbolic functions.
  • Statistics — transform data in advanced statistical pipelines that use hyperbolic math.
  • Signal processing — complementary to circular trig when analyzing non-periodic exponential signals.

🧠 How $cosh Works

1

MongoDB reads the input

The pipeline evaluates the expression — a field like "$x" or a numeric literal.

Input
2

$cosh applies the formula

MongoDB computes (ex + e-x) / 2 and returns a value ≥ 1.

Transform
3

The result is stored in the pipeline

The hyperbolic cosine is written to the field you define in $project or $addFields.

Output
=

Hyperbolic value ready to use

Feed into $acosh for the inverse, or combine with $sinh for full hyperbolic identities.

Conclusion

The $cosh operator brings hyperbolic cosine math into MongoDB aggregation pipelines. Remember that output is always at least 1, values grow exponentially for large inputs, and the function is symmetric around zero.

Do not confuse $cosh with $cos (circular, bounded) or $acosh (inverse, requires input ≥ 1). Pick the operator that matches your mathematical model.

💡 Best Practices

✅ Do

  • Remember output is always ≥ 1
  • Use $acosh when you need the inverse operation
  • Test with cosh(0) = 1 to validate pipelines
  • Leverage the even-function property for signed data
  • Pair with $sinh for hyperbolic identity calculations

❌ Don’t

  • Confuse $cosh with $cos (different math entirely)
  • Expect bounded output like circular cosine (-1 to 1)
  • Use $acosh on $cosh output without checking the domain
  • Assume small inputs stay near 1 — growth accelerates quickly
  • Forget that null input returns null

Key Takeaways

Knowledge Unlocked

Five things to remember about $cosh

Use these points when working with hyperbolic cosine in MongoDB.

5
Core concepts
📝 02

{ $cosh: x }

One expression.

Syntax
📏 03

Output ≥ 1

Min at cosh(0).

Range
🛠 04

Even Function

cosh(-x) = cosh(x).

Property
05

Not $cos

Hyperbolic vs circular.

Important

❓ Frequently Asked Questions

$cosh returns the hyperbolic cosine of a numeric value. Unlike circular cosine, hyperbolic cosine grows exponentially and is always greater than or equal to 1.
The syntax is { $cosh: <expression> }. The expression can be a field reference like "$x", a literal number, or another numeric expression.
$cos is circular trigonometry (output -1 to 1, periodic). $cosh is hyperbolic (output >= 1, grows exponentially). They solve different math problems.
Yes. cosh(-x) equals cosh(x). In a pipeline, $cosh applied to a negative field returns the same result as the positive value.
$cosh returns null when the input is null. It does not throw an error.

Continue the Operator Series

Move on to $dateAdd for date arithmetic, or review $cos for circular trigonometry.

Next: $dateAdd →

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