The $ln operator calculates the natural logarithm of a number in an aggregation expression. It uses base e (Euler’s number, about 2.71828) — the same as Math.log() in JavaScript.
01
Natural Log
Logarithm base e.
02
Syntax
{ $ln: expr }.
03
Positive Input
Values must be > 0.
04
Key Values
ln(1)=0, ln(e)=1.
05
Use Cases
Growth, science, stats.
06
vs $log
Base e vs custom base.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $ln operator computes the natural logarithm of a numeric expression. For example, ln(1) = 0 and ln(e) ≈ 1. This is useful when working with exponential growth, concentration scales, entropy calculations, or any formula that uses base-e logarithms.
Think of $ln as MongoDB’s pipeline version of JavaScript’s Math.log() — not Math.log10(), which uses base 10.
💡
Beginner Tip
Natural logarithm answers the question: “To what power must e be raised to get this number?” For example, because e¹ ≈ 2.718, we know ln(2.718) ≈ 1.
// Sample A (1/2=0.5) → logGrowth: -0.693 (decline)
// Sample B (2.718/2) → logGrowth: 0.306 (growth)
// Sample C (10/2=5) → logGrowth: 1.609 (strong growth)
How It Works
ln(current / baseline) expresses relative change on a log scale. Positive values mean growth above the baseline; negative values mean decline. This pattern is common in economics and biology.
Example 3 — $ln with Literal Numbers
Compute natural logarithms for fixed constants in a projection:
Natural logarithm is only defined for positive numbers. If your data might include zero or negative values, check with $gt before calling $ln, and return null or a default when the input is invalid.
This is equivalent to Example 2’s ln(concentration / 2) thanks to the logarithm property: ln(a) - ln(b) = ln(a/b). Choose whichever form reads clearer in your pipeline.
Applications
🚀 Use Cases
Scientific data — transform concentration or intensity values onto a log scale.
Growth analysis — compute log returns and proportional change from ratios.
Statistics — prepare data for models that assume log-normal distributions.
Entropy & information theory — formulas often use natural logarithms.
🧠 How $ln Works
1
MongoDB evaluates the input
The expression inside $ln resolves to a number from a field, literal, or nested operator.
Input
2
Checks for null
If the input is null, $ln returns null without error.
Null
3
Computes natural log
For positive numbers, MongoDB returns logₑ(x) — the power you raise e to in order to get x.
Compute
=
📊
Log-scaled output
Your pipeline gets the natural logarithm, ready for further math or reporting.
Wrap Up
Conclusion
The $ln operator brings natural logarithm calculations into MongoDB aggregation pipelines. Use it when your formulas require base-e logs — the same family as JavaScript’s Math.log().
Remember the domain: input must be positive. Guard with $cond when data quality is uncertain, and reach for $log when you need a custom logarithm base.
Ensure input values are positive before applying $ln
Use $cond to handle zero, negative, or missing values
Remember ln(1) = 0 as a quick sanity check
Use $log when you need base 10 or another custom base
Combine with $divide for log-ratio growth calculations
❌ Don’t
Apply $ln to zero or negative numbers without guarding
Confuse $ln (base e) with base-10 logarithm
Use $ln as a pipeline stage — it is an expression operator
Expect meaningful results from non-numeric strings
Forget that null input returns null
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $ln
Use these points when working with natural logarithms in MongoDB.
5
Core concepts
📝01
Natural Log
Base e logarithm.
Purpose
🔢02
ln(1) = 0
Key reference.
Math
🛠03
Positive Only
Guard invalid input.
Domain
🔄04
Log Ratios
ln(a/b) growth.
Pattern
📑05
vs $log
Custom base next.
Related
❓ Frequently Asked Questions
$ln returns the natural logarithm of a number — the logarithm with base e (approximately 2.71828). It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $ln: <expression> }. The expression can be a field reference like "$value", a literal positive number, or another numeric expression.
$ln requires a positive number. ln(1) is 0. For null input, $ln returns null. Zero and negative numbers are not valid for natural logarithm and typically produce null or an error depending on context.
$ln computes the natural logarithm (base e). $log lets you specify a custom base as a second argument. Use $ln when you specifically need base-e logarithms.
Use $ln inside expression stages such as $project, $addFields, $set, and within other numeric expressions when transforming scientific, financial, or statistical data.