$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.

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $ln: <numeric-expression> }

⚙️ Parameters

numeric valueA 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏