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.
Fundamentals
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.
Foundation
📝 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: 0 → 1(e° = 1)
$exp: 1 → ≈ 2.718(value of e)
$exp: 2 → ≈ 7.389(e²)
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (math)
Syntax
{ $exp: <expression> }
Formula
ex where e ≈ 2.718
Output
Positive 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
Hands-On
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:
$exp(0) = 1, $exp(1) ≈ 2.718, and $exp(-1) ≈ 0.368. Use these to validate pipeline logic or as constants in formulas.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $exp
Use these points when working with exponential math in MongoDB.
5
Core concepts
🔢01
Natural Exponential
Computes ex.
Purpose
📝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.