MongoDB $ln Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $ln?

$ln is a MongoDB aggregation expression that returns the natural logarithm of a number—log base e (about 2.71828). The input must be a positive number.

Remember
$ln = Math.log for aggregation (base e)

{ $ln: <expression> }

ln(1) → 0
ln(e) → 1
input → must be > 0

null / missing → null
0 or negative → error

inverse → $exp
UsePrefer
Natural log (base e)$ln
Log with a chosen base$log
Reverse natural log$exp

How to Use $ln in MongoDB

Pass a positive number expression and emit the natural log.

  1. Choose an expression stage ($addFields or $project).
  2. Confirm the value is greater than 0 (filter or validate first if needed).
  3. Pass the field or expression to $ln.
  4. Use $exp when you need the inverse.
PieceRequired?Role
ExpressionYesPositive number, field, or nested math
DomainYesMust be > 0
$expNoUndo $ln (round-trip)

Pseudocode

Pseudocode
for each document:
    x = evaluate(expression)
    if x is null or missing:
        result = null
    else if x <= 0:
        error  // natural log undefined
    else:
        result = ln(x)   // base e
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while using natural logs in pipelines.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $ln: <expression> }
BaseAlways e ≈ 2.71828
vs $log$log chooses the base
DomainInput must be > 0
Handy valuesln(1)=0 · ln(e)≈1
Inverse$exp

Live Preview

Watch $ln on common positive values. Zero and negatives are invalid.

Simulates { $ln: <value> } (natural log, base e).

Result
value = 2.718281828459045 (e)

$ln → 1

{ naturalLog: 1 }

Worked Walkthrough — ln(e)

A sample stores e (about 2.718). The natural log of e is exactly 1—the cleanest check that $ln is working.

Calculation
Document:
  { label: "unit-e", value: Math.E }   // ≈ 2.71828

$ln("$value")
  = ln(e)
  ≈ 1

Handy checks:
  ln(1) = 0
  ln(e) = 1
  $exp(1) ≈ e   // inverse

MongoDB Examples

We use one sample value equal to Math.E. First insert it, then compute $ln, then show ln(1) beside it. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one measurement whose value is e ((Math.E ≈ 2.71828). We save it in a collection named samples.

mongosh
db.samples.insertOne({
  _id: 1,
  label: "unit-e",
  value: Math.E
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in samples. Run find() to see it:

mongosh
db.samples.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id
label"unit-e"What this sample is
valueMath.E ≈ 2.718Number for $ln
Picture
value ≈ 2.71828  (= e)

$ln(value) → 1

because e¹ = e

How It Works

  1. Insert once. insertOne adds a single sample to samples.
  2. Store e. Math.E in mongosh is the base of natural logs.
  3. find() shows the number. Ready for $ln next.

Example 2 — Natural Log

Goal: add naturalLog = $ln of value (expect about 1).

mongosh
db.samples.aggregate([
  {
    $addFields: {
      naturalLog: { $ln: "$value" }
    }
  }
]);

How It Works

  1. Read the stored value. MongoDB already has value ≈ e from Example 1.
  2. Apply natural log. $ln computes log base e.
  3. Store the result. $addFields writes naturalLog: 1.

Example 3 — ln(e) vs ln(1)

Same sample, plus ln(1) as a second field so you can see the two classic values side by side.

mongosh
db.samples.aggregate([
  {
    $addFields: {
      lnOfValue: { $ln: "$value" },
      lnOfOne: { $ln: 1 }
    }
  }
]);

How It Works

  1. Same stored document. Still using value ≈ e from Example 1.
  2. ln(e). $ln: "$value" returns 1.
  3. ln(1). The literal 1 always logs to 0—a useful sanity check.

Edge Cases & Pitfalls

Check these before using $ln on production data.

domain

Only positive numbers

Zero and negatives are invalid for natural log and cause an error. Filter with $gt: 0 first when data may be dirty.

null

Null stays null

Missing or null inputs return null. They do not become zero.

base

Need another base?

Use $log (or $log10). $ln is always base e.

float

Floating-point results

ln(e) is exactly 1 in theory; other inputs return floats. Round only when your report needs fixed decimals.

Performance Notes

ApproachCostBest for
$ln per documentCheap mathOn-the-fly transforms
Filter then $lnFewer docsDirty or sparse data
Store log on writeNo recomputeHot dashboards

Natural log is inexpensive. Filter invalid or unused documents early with $match when only some rows need a log.

Key Takeaways

  • Role: natural logarithm (base e).
  • Domain: input must be > 0.
  • vs $log: $ln is fixed base e; $log chooses a base.
  • Sample: ln(e) → 1, ln(1) → 0.

One line: $ln answers “what power of e gives this number?”—for positive inputs only.

Frequently Asked Questions

$ln returns the natural logarithm of a number—the logarithm with base e (about 2.71828). Use it inside aggregation stages such as $project and $addFields.
Use { $ln: <expression> }. Pass a field like "$value", a positive number literal, or any expression that resolves to a positive number.
$ln needs a positive number. ln(1) is 0 and ln(e) is about 1. Null or missing input returns null. Zero or negative values cause an error.
$ln is always base e. $log lets you choose a base as a second argument. Use $ln when you specifically need the natural log.
Use $exp. If y = $ln(x), then $exp(y) returns x (for valid positive x).
Anywhere an aggregation expression is allowed—$addFields, $project, $set, and inside other numeric expressions.

Next: $log

Compute a logarithm with any base you choose.

$log tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful