$ln (Natural Log)
Last updated: 2026-10-09
$ln (Natural Log)
$ln returns the natural logarithm (base e) of a number. It is used for growth-rate calculations, data smoothing, and log-scale transforms, such as taking the natural log of prices before regression analysis. A null or missing argument yields null; the input must be positive — zero or negative values return null. e is approximately 2.71828. For other bases use $log with an explicit base, or the dedicated $log10 and $log2. $ln is the inverse of $exp.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $ln: <numeric-expression> }
⚙️ Parameters
| numeric value | A numeric value or an expression resolving to one; must be greater than 0. |
|---|
Returns:The natural logarithm of the input; null for non-positive, null, or missing input
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $ln: take the natural log of amounts
db.sales.aggregate([
{ $project: {
logAmount: { $ln: '$amount' }
} }
])
Output:
{ '_id': 1, 'logAmount': 4.605170185988092 }
{ '_id': 2, 'logAmount': 2.995732273553991 }
📌 Notes
- The input must be greater than 0; zero or negative values return null
- The base is fixed at e; use $log for other bases
- Inverse of $exp: $exp($ln(x)) equals x
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $ln and $log?
A$ln uses a fixed base of e; $log takes the base as its second argument.
QWhat does $ln return for zero or negative input?
AIt returns null; logarithms are only defined for positive numbers.
QHow do I restore the original value from a natural log?
AApply $exp to the result to invert the logarithm.