MongoDB $exp Operator

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

What You’ll Learn

The $exp operator computes ex (Euler’s number raised to a power) in MongoDB aggregation pipelines. It is the natural exponential function, widely used for compound growth, decay curves, and probability modeling.

01

Natural Exponential

Compute ex in pipelines.

02

Syntax

One expression inside $exp.

03

$project Stage

Add computed growth fields.

04

Field References

Apply to document fields.

05

Use Cases

Growth, decay, finance.

06

Inverse of $ln

Pairs with natural log.

Definition and Usage

In MongoDB’s aggregation framework, the $exp operator raises Euler’s number e (approximately 2.718) to the power of a numeric expression. For example, { $exp: 0 } returns 1, and { $exp: 1 } returns approximately 2.718. This is the same as JavaScript’s Math.exp() and is essential for continuous growth and decay formulas.

💡
Beginner Tip

Think of $exp as computing ex. Use it inside expression stages like $project or $addFields. For general base-to-power math (like 210), use $pow instead.

📝 Syntax

The $exp operator takes one numeric expression (the exponent):

mongosh
{ $exp: <expression> }

Syntax Rules

  • $exp — returns e raised to the power of the expression (ex).
  • <expression> — can be a field path ("$rate"), a literal number, or another numeric expression.
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
  • Large positive exponents produce very large numbers; large negative exponents approach zero.

📐 Common $exp Values to Remember

These reference values help validate pipeline output:

$exp: 01 (e° = 1)
$exp: 1≈ 2.718 (value of e)
$exp: 2≈ 7.389 (e²)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (math)
Syntax{ $exp: <expression> }
Formulaex where e ≈ 2.718
OutputPositive number (or null)
Common stages$project, $addFields, $set
Example
{
  $exp: 2
}

≈ 7.389 (e²)

Field
{
  $exp: "$rate"
}

e raised to rate

Zero
{
  $exp: 0
}

Returns 1

Null input
{
  $exp: null
}

Returns null

Examples Gallery

Walk through sample growth data, compute exponential values with $project, and see how ex applies to real-world formulas.

📚 Natural Exponential

Start with a metrics collection and compute ex for stored rate values using $project.

Sample Input Documents

Suppose you have a metrics collection with a rate field representing an exponent:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "label": "Baseline", "rate": 0 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "label": "Unit growth", "rate": 1 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "label": "Double growth", "rate": 2 }
]

Example 1 — Basic $exp on a Field

Compute erate for each document:

mongosh
db.metrics.aggregate([
  {
    $project: {
      label: 1,
      rate: 1,
      expValue: { $exp: "$rate" }
    }
  }
])

How It Works

  • Document 1: e&sup0; = 1
  • Document 2: e¹ ≈ 2.718
  • Document 3: e² ≈ 7.389

📈 Practical Patterns

Apply $exp to growth formulas, decay curves, and combined math in pipelines.

Example 2 — Continuous Compound Growth

Calculate a growth multiplier using the formula P × ert, where r is the rate and t is time:

mongosh
db.investments.aggregate([
  {
    $project: {
      principal: 1,
      rate: 1,
      years: 1,
      growthFactor: {
        $exp: {
          $multiply: [ "$rate", "$years" ]
        }
      },
      futureValue: {
        $multiply: [
          "$principal",
          { $exp: { $multiply: [ "$rate", "$years" ] } }
        ]
      }
    }
  }
])

How It Works

Multiply rate by time first with $multiply, then pass the result to $exp. Multiply by principal to get the future value.

Example 3 — Exponential Decay with $addFields

Model decay with a negative exponent: e-λt:

mongosh
db.sensors.aggregate([
  {
    $addFields: {
      decayFactor: {
        $exp: {
          $multiply: [ -1, "$decayRate", "$elapsedTime" ]
        }
      }
    }
  }
])

How It Works

Negative exponents produce values between 0 and 1, modeling how a signal or substance decreases over time.

Example 4 — $exp with Literal Values

Common reference values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      expZero:  { $exp: 0 },    // 1
      expOne:   { $exp: 1 },    // ≈ 2.718
      expNeg:   { $exp: -1 }    // ≈ 0.368
    }
  }
])

How It Works

$exp(0) = 1, $exp(1) ≈ 2.718, and $exp(-1) ≈ 0.368. Use these to validate pipeline logic or as constants in formulas.

🚀 Use Cases

  • Continuous compound growth — calculate future values with the formula P × ert in finance pipelines.
  • Exponential decay — model signal loss, radioactive decay, or cooling with negative exponents.
  • Probability and statistics — compute softmax weights, log-normal distributions, and other e-based formulas.
  • Scientific computing — transform logarithmic data back to linear scale using ex.

🧠 How $exp Works

1

MongoDB reads the exponent

The pipeline evaluates the input — a field like "$rate" or a numeric expression such as { $multiply: [ "$rate", "$years" ] }.

Input
2

$exp computes ex

MongoDB raises e (approximately 2.718) to the power of the exponent and returns the result.

Transform
3

The result is stored in the pipeline

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

Output
=

Exponential values ready

You get growth factors, decay rates, or transformed values for charts, forecasts, and scientific calculations.

Conclusion

The $exp operator brings the natural exponential function into MongoDB aggregation pipelines. It is essential for continuous growth and decay formulas, financial projections, and any calculation that requires raising e to a power.

For beginners, the key idea is simple: wrap any numeric exponent in { $exp: ... } inside a stage like $project, and MongoDB returns ex. Remember that $exp(0) = 1 and $exp(1) ≈ 2.718.

💡 Best Practices

✅ Do

  • Use $exp for ex (natural exponential) calculations
  • Combine with $multiply for formulas like ert
  • Use $round when displaying results to users
  • Pair with $ln for log/exp round-trip transforms
  • Test with 0, 1, and -1 to validate pipeline logic

❌ Don’t

  • Use $exp when you need $pow with base 2 or 10
  • Pass very large exponents without considering overflow
  • Use $exp as a query filter outside expressions
  • Assume it converts non-numeric strings to numbers
  • Forget that null input returns null

Key Takeaways

Knowledge Unlocked

Five things to remember about $exp

Use these points when working with exponential math in MongoDB.

5
Core concepts
📝 02

Simple Syntax

{ $exp: expr }

Syntax
🛠 03

Pipeline Stages

$project, $addFields, $set.

Usage
💰 04

Growth & Decay

Finance and science.

Use case
05

Key Values

exp(0)=1, exp(1)≈2.718.

Edge case

❓ Frequently Asked Questions

$exp raises Euler's number e (approximately 2.718) to the power of a numeric expression. It is the natural exponential function, useful for growth, decay, and probability calculations in aggregation pipelines.
The syntax is { $exp: <expression> }. The expression can be a field reference like "$rate", a literal number, or another numeric expression.
$exp(0) returns 1, because any number raised to the power of 0 is 1. $exp(1) returns approximately 2.718 (the value of e).
$exp returns null when the input is null. It does not throw an error.
$exp and $ln are inverse operations. $ln is the natural logarithm, and $exp($ln(x)) returns x for positive values. Use $exp for e^x and $ln for log base e.

Continue the Operator Series

Move on to $expr for aggregation expressions in queries, or review $exists for field presence checks.

Next: $expr →

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