MongoDB $exp Operator

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

What Is $exp?

$exp is a MongoDB aggregation expression that raises Euler’s number e (about 2.718) to a power. In short: $exp(x) means ex. Use it for growth, decay, and continuous compounding math in the pipeline.

Remember
$exp = Math.exp for aggregation

{ $exp: <expression> }

$exp(0) → 1
$exp(1) → e ≈ 2.718
$exp(2) → ≈ 7.389

null / missing → null
NaN → NaN
UsePrefer
Natural exponential ex$exp
Natural log (inverse of $exp)$ln
Any base power (ab)$pow

How to Use $exp in MongoDB

Pass a numeric exponent (or an expression that evaluates to one).

  1. Choose an expression stage ($addFields or $project).
  2. Provide a number, field, or nested math as the exponent.
  3. Pass that expression to $exp to get ex.
  4. Optionally combine with $subtract or $multiply for rates and growth formulas.
PieceRequired?Role
$expYesComputes ex
ExpressionYesNumeric exponent (field, literal, or nested math)
$subtract / $multiplyNoBuild formulas like effective rate or future value

Pseudocode

Pseudocode
for each document:
    x = evaluate(expression)
    if x is null or missing:
        result = null
    else if x is NaN:
        result = NaN
    else:
        result = e ^ x
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing exponential expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $exp: <expression> }
Meaningex (e ≈ 2.71828)
Return typeDouble by default; decimal if input is decimal
Key values$exp(0)=1, $exp(1)≈2.718, $exp(2)≈7.389
Continuous rate$subtract: [{ $exp: "$rate" }, 1]
Inverse$ln (natural log)

Live Preview

Watch $exp raise e to a power. Try common exponents.

Simulates $exp (same idea as the mongosh examples below).

Result
x = 1
$exp(x) → 2.718282

{ result: 2.718282 }

Worked Walkthrough — Continuous Rate

An account has interest rate 0.08 (8%). For continuous compounding, the effective rate is er − 1.

Calculation
Document:
  { interestRate: 0.08, presentValue: 10000 }

$exp(0.08) ≈ 1.083287

effectiveRate = $exp(rate) − 1
              ≈ 1.083287 − 1
              ≈ 0.083287  (about 8.33%)

Check: slightly above the nominal 8% ✓

MongoDB Examples

We use a small accounts collection. First we raise e to a rate, then compute an effective continuous rate. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine three savings accounts. Each has an interest rate and a present value. We save them in a collection named accounts.

mongosh
db.accounts.insertMany([
  { _id: 1, interestRate: 0.08, presentValue: 10000 },
  { _id: 2, interestRate: 0.0825, presentValue: 250000 },
  { _id: 3, interestRate: 0.0425, presentValue: 1000 }
]);

How the Data Is Stored in MongoDB

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

mongosh
db.accounts.find()

Read one document like this:

Field in MongoDBValue (doc 1)Plain English
_id1Document id
interestRate0.08Nominal rate (8%)
presentValue10000Current balance

How It Works

  1. Insert once. insertMany adds three account documents to accounts.
  2. Rates are decimals. 0.08 means 8%, not the number 8.
  3. find() shows the same shape. What you inserted is what you get back—ready for $exp next.

Example 2 — Raise e to a Rate

Goal: compute einterestRate for each account.

mongosh
db.accounts.aggregate([
  {
    $addFields: {
      eToRate: { $exp: "$interestRate" }
    }
  }
]);

How It Works

  1. Read the stored rate. For document 1, interestRate is 0.08.
  2. Raise e. $exp("$interestRate") computes e0.08 ≈ 1.0833.
  3. Repeat per document. Each account gets its own eToRate from its own rate.
  4. Keep all fields. $addFields adds eToRate next to the original fields.

Example 3 — Effective Continuous Rate

Same accounts, but convert to an effective rate with er − 1.

mongosh
db.accounts.aggregate([
  {
    $project: {
      effectiveRate: {
        $subtract: [
          { $exp: "$interestRate" },
          1
        ]
      }
    }
  }
]);

How It Works

  1. Same stored rates. Still using 0.08, 0.0825, and 0.0425 from Example 1.
  2. Compute er. $exp("$interestRate") → about 1.0833 for the first account.
  3. Subtract 1. $subtract turns that into ≈ 0.0833 (about 8.33%).
  4. $project keeps only what you ask for. Here: _id and effectiveRate.

Edge Cases & Pitfalls

Check these before using $exp in production pipelines.

units

Rates are decimals

Pass 0.08 for 8%, not 8. $exp(8) is huge (~2980), not a rate factor.

null

Null and missing become null

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

overflow

Very large exponents

Huge positive exponents can overflow to infinity. Clamp or validate inputs when user data can be extreme.

base

$exp is only base e

For ab with another base, use $pow, not $exp.

Performance Notes

ApproachCostBest for
$exp alonePer-document mathStored exponents / rates
$exp + $subtractStill O(docs)Effective continuous rates
+ other arithmeticOne extra op eachGrowth / decay formulas

Exponential math is cheap compared with large sorts or joins. Filter early with $match when only some documents need $exp.

Key Takeaways

  • Role: $exp(x) = ex.
  • Anchors: $exp(0)=1, $exp(1)≈2.718.
  • Rates: er − 1 for continuous compounding.
  • Inverse: use $ln for natural log.

One line: $exp answers “what is e raised to this power?”—in aggregation, like Math.exp.

Frequently Asked Questions

$exp raises Euler's number e (about 2.718) to the power of a number. It is the natural exponential function e^x. Use it inside aggregation expression stages such as $project and $addFields.
The syntax is { $exp: <expression> }. The expression can be a field reference, a literal number, or nested numeric math.
$exp(0) returns 1. $exp(1) returns e, approximately 2.718281828459045.
$exp returns null when the input is null or the field is missing. If the argument is NaN, $exp returns NaN.
They are inverse operations. $ln is the natural logarithm. For positive x, $exp($ln(x)) returns x (within floating-point precision).
Yes for the math. Think of $exp as MongoDB’s aggregation version of Math.exp—run it in the pipeline, not as a standalone find() filter.

Next: $expr

Use aggregation expressions inside find queries and $match.

$expr 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