$exp (Exponential)
Last updated: 2026-10-09
$exp (Exponential)
$exp returns e raised to the given power, the exponential function e to the power of x, where e is approximately 2.71828. It is used for continuous compounding, exponential smoothing, and undoing natural logarithms. A null or missing argument yields null. $exp and $ln are inverses: $ln($exp(x)) equals x. Note that large exponents can overflow to Infinity, so scale the input with $divide beforehand to avoid losing precision.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $exp: <numeric-expression> }
⚙️ Parameters
| exponent | The exponent expression; it may be positive or negative. |
|---|
Returns:e raised to the power of the input; null when null or missing
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $exp: restore original values from natural logs
db.sales.aggregate([
{ $project: {
restored: { $exp: '$logAmount' }
} }
])
Output:
{ '_id': 1, 'restored': 100 }
{ '_id': 2, 'restored': 20 }
📌 Notes
- It is the inverse of $ln
- Very large exponents overflow to Infinity
- Null or missing arguments yield null
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow are $exp and $ln related?
AThey are inverses, so $ln($exp(x)) restores x.
QWhat happens when $exp overflows?
AThe result becomes Infinity for huge exponents; scale the input down first.
QWhat is $exp(0)?
AIt is 1, since e to the power of zero is 1.