MongoDB $cosh Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $cosh?

$cosh is a MongoDB aggregation expression that returns the hyperbolic cosine of a number. The input is measured in radians. For real numbers, the result is always ≥ 1.

Remember
$cosh = Math.cosh for aggregation

{ $cosh: <expression> }

input  → radians (any real number)
output → ≥ 1  (grows as |x| grows)
even   → cosh(-x) = cosh(x)

cosh(0) → 1
null / missing → null
NaN → NaN
UsePrefer
Hyperbolic cosine$cosh
Inverse hyperbolic cosine$acosh
Regular (circular) cosine$cos

How to Use $cosh in MongoDB

Pass a numeric expression (radians). Convert degrees first if that is how your angle is stored.

  1. Choose an expression stage ($addFields or $project).
  2. If the value is in degrees, convert with $degreesToRadians.
  3. Pass that expression to $cosh.
  4. Use the result (≥ 1) as a field, in formulas, or with other hyperbolic ops.
PieceRequired?Role
$coshYesHyperbolic cosine of a radians value
ExpressionYesNumber, field, or nested math
$degreesToRadiansNoConvert degree angles before $cosh

Pseudocode

Pseudocode
for each document:
    x = evaluate(expression)   // radians
    if x is null or missing:
        result = null
    else if x is NaN:
        result = NaN
    else:
        result = cosh(x)       // ≥ 1 for real x
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing hyperbolic-cosine expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $cosh: <expression> }
Input unitRadians
Output≥ 1 for real numbers
Even propertycosh(-x) = cosh(x)
Sample checkscosh(0) = 1; cosh(1) ≈ 1.543
vs $cosHyperbolic (≥ 1) vs circular ([-1, 1])

Live Preview

Watch $cosh turn a number into its hyperbolic cosine. Try negatives to see the even property.

Simulates $cosh (same idea as the mongosh examples below). Note cosh(1) = cosh(-1).

Result
x = 1
$cosh(x) → 1.543081

{ y: 1.543081 }

Worked Walkthrough — cosh(0), cosh(1), cosh(-1)

Hyperbolic cosine is even: the sign of x does not change the result. Zero is the minimum.

Calculation
cosh(x) = (e^x + e^(-x)) / 2

x = 0
  cosh(0) = (1 + 1) / 2 = 1

x = 1
  cosh(1) ≈ 1.543081

x = -1
  cosh(-1) ≈ 1.543081   ← same as cosh(1)

x = 2
  cosh(2) ≈ 3.762196

Minimum on the real line: cosh(0) = 1

MongoDB Examples

We use a small hyperbolic collection with values of x. First we compute $cosh, then we highlight the even property. Insert the sample data once in mongosh.

Example 1 — Sample Data

Imagine four numbers stored as x: 0, 1, -1, and 2. We save them in a collection named hyperbolic.

mongosh
db.hyperbolic.insertMany([
  { _id: 1, x: 0 },
  { _id: 2, x: 1 },
  { _id: 3, x: -1 },
  { _id: 4, x: 2 }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps four documents in hyperbolic. Run find() to see them:

mongosh
db.hyperbolic.find()

Read those documents like this:

Field in MongoDBExamplePlain English
_id1 … 4Document id for this lesson
x1 or -1Input value for $cosh (radians)
Picture
Hyperbolic cosine curve (sketch):

  y
  |         /
  |        /
  |   ____/____   ← minimum at cosh(0) = 1
  |  /         \
  | /
  +--------------→ x
     -2  -1  0  1  2

  cosh(-1) = cosh(1)  (mirror / even)

How It Works

  1. Insert once. insertMany adds four documents with different x values.
  2. MongoDB stores numbers as written. Including the negative -1.
  3. find() shows the same shape. What you inserted is what you get back—ready for $cosh next.

Example 2 — Hyperbolic Cosine

Goal: add a y field with $cosh of each stored x.

mongosh
db.hyperbolic.aggregate([
  {
    $addFields: {
      y: { $cosh: "$x" }
    }
  }
]);

How It Works

  1. Read x. For each document, "$x" is the input to $cosh.
  2. Compute cosh. 0 → 1, 1 → ≈1.543, 2 → ≈3.762.
  3. Negatives match. x: -1 produces the same y as x: 1.
  4. Keep all rows. $addFields adds y; originals stay.

Example 3 — Even Property Check

Keep only x = 1 and x = -1 to make the even property obvious side by side.

mongosh
db.hyperbolic.aggregate([
  { $match: { x: { $in: [1, -1] } } },
  {
    $addFields: {
      y: { $cosh: "$x" }
    }
  }
]);

How It Works

  1. Same stored documents. Still using the rows from Example 1.
  2. Filter to a pair. $match keeps only 1 and -1.
  3. Same $cosh. Both produce ≈ 1.54308.
  4. Takeaway. You can flip the sign of x without changing $cosh—useful when only magnitude matters.

Edge Cases & Pitfalls

Check these before using $cosh in production pipelines.

units

Input is radians

If your field is in degrees, convert with $degreesToRadians first—same rule as $cos.

growth

Grows quickly

cosh increases fast as |x| grows. Very large inputs can become huge doubles.

null

Null and missing become null

Missing fields return null, not an error. Handle them before hyperbolic math if nulls are invalid.

inverse

Need the inverse? Use $acosh

$cosh goes x → y. To go y → x (for y ≥ 1), use $acosh.

Performance Notes

ApproachCostBest for
$cosh alonePer-document mathValues already in radians
$degreesToRadians + $coshStill O(docs)Degree-based angle fields
With $acoshTwo trig opsRound-trip checks / transforms

Hyperbolic expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need $cosh.

Key Takeaways

  • Role: hyperbolic cosine → value ≥ 1.
  • Units: input treated as radians.
  • Even: cosh(-x) = cosh(x).
  • vs cos: use $cos for circular trig in [-1, 1].

One line: $cosh answers “what is the hyperbolic cosine of this value?”—always at least 1 for real inputs, and even in x.

Frequently Asked Questions

$cosh returns the hyperbolic cosine of a number. The input is treated as radians. The result is always ≥ 1 for real numbers. Use it inside aggregation expression stages such as $project and $addFields.
Use { $cosh: <expression> }. The expression can be a field path, a literal number, or nested math.
$cos is circular trigonometry (output between -1 and 1, periodic). $cosh is hyperbolic (output ≥ 1, grows as |x| grows). They solve different problems.
Yes. cosh(-x) equals cosh(x). Positive and negative inputs with the same absolute value produce the same result.
$cosh returns null when the input is null or the field is missing. If the argument is NaN, $cosh returns NaN.
Yes for the math. Think of $cosh as MongoDB’s aggregation version of Math.cosh—run it in the pipeline, not as a standalone find() filter.

Next: $dateAdd

Add years, months, days, or other units to a date value.

$dateAdd tutorial →

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.

9 people found this page helpful