MongoDB $log10 Operator

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Math

What You’ll Learn

The $log10 operator calculates the base-10 logarithm of a number in an aggregation expression. It is a convenient shortcut when you need common logarithms — the same as { $log: [ number, 10 ] } but simpler to write.

01

Base 10

Common logarithm.

02

Syntax

{ $log10: expr }.

03

Powers of 10

100 → 2, 1000 → 3.

04

vs $log

Shortcut for base 10.

05

Use Cases

Scaling, decibels.

06

Domain Rules

Positive numbers only.

Definition and Usage

In MongoDB’s aggregation framework, the $log10 operator computes log₁₀(number) — the power you must raise 10 to in order to get the number. For example, log₁₀(100) = 2 because 10² = 100, and log₁₀(1000) = 3 because 10³ = 1000.

Think of $log10 as MongoDB’s pipeline version of JavaScript’s Math.log10(). When you only need base 10, it is cleaner than writing { $log: [ "$value", 10 ] }.

💡
Beginner Tip

Powers of 10 give clean integer results: log₁₀(10) = 1, log₁₀(100) = 2, log₁₀(1000) = 3. This makes base-10 logs easy to interpret when scaling large numbers.

📝 Syntax

The $log10 operator takes one numeric expression:

mongosh
{ $log10: <expression> }

Common Patterns

mongosh
// Field reference
{ $log10: "$value" }

// Literal number
{ $log10: 100 }

// Equivalent $log form
{ $log: [ "$value", 10 ] }

Syntax Rules

  • $log10 — returns the base-10 logarithm of the expression.
  • <expression> — must evaluate to a positive number for a valid result.
  • log₁₀(1) = 0 — the log of 1 is always zero.
  • null input — returns null.
  • Use inside stages like $project, $addFields, or $set.
  • Equivalent to { $log: [ number, 10 ] } when base is always 10.

💡 $log10 vs $log vs $ln

{ $log10: 100 }2
{ $log: [ 100, 10 ] }2 (same result)
{ $ln: 100 }≈ 4.605170 (base e, different)
Choose $log10 when you always need base 10

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (math)
Syntax{ $log10: <expression> }
Base10 (fixed)
Valid inputPositive numbers (and nullnull)
Equivalent{ $log: [ number, 10 ] }
log10(10)
{
  $log10: 10
}

Returns 1

Field
{
  $log10: "$value"
}

Log of field

log10(100)
{
  $log10: 100
}

Returns 2

Null
{
  $log10: null
}

Returns null

Examples Gallery

Walk through a data collection with large numeric values and compute base-10 logarithms step by step.

📚 Large Numeric Values

Start with a data collection and compute log₁₀(value) with $project.

Sample Input Documents

Suppose you have a data collection with a value field:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "label": "Small", "value": 100 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "label": "Medium", "value": 1000 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "label": "Large", "value": 10000 }
]

Example 1 — Basic $log10 on a Field

Calculate the base-10 logarithm of each document’s value:

mongosh
db.data.aggregate([
  {
    $project: {
      label: 1,
      value: 1,
      log10Value: { $log10: "$value" }
    }
  }
])

How It Works

  • Small (100): log₁₀(100) = 2 because 10² = 100.
  • Medium (1000): log₁₀(1000) = 3 because 10³ = 1000.
  • Large (10000): log₁₀(10000) = 4 because 10&sup4; = 10000.

📈 Practical Patterns

Use $log10 for scaling, comparisons, and safe transformations.

Example 2 — Compress Large Numbers for Charts

Map wide-ranging values onto a smaller log scale for visualization or bucketing:

mongosh
db.data.aggregate([
  {
    $project: {
      label: 1,
      value: 1,
      logScale: { $log10: "$value" },
      magnitude: {
        $floor: { $log10: "$value" }
      }
    }
  }
])

How It Works

$floor on $log10 gives the order of magnitude — how many digits (in base 10) the number has minus one. Useful for grouping values like 100–999 into magnitude 2.

Example 3 — $log10 vs $log Equivalence

Verify that $log10 matches $log with base 10:

mongosh
db.data.aggregate([
  {
    $project: {
      label: 1,
      value: 1,
      viaLog10: { $log10: "$value" },
      viaLog: {
        $log: [ "$value", 10 ]
      }
    }
  }
])

How It Works

Prefer $log10 when base is always 10 — it is shorter and clearer. Use $log when the base varies per document or formula.

Example 4 — Guard Against Invalid Input

Apply $log10 only when the value is positive:

mongosh
db.data.aggregate([
  {
    $project: {
      label: 1,
      value: 1,
      safeLog10: {
        $cond: [
          { $gt: [ "$value", 0 ] },
          { $log10: "$value" },
          null
        ]
      }
    }
  }
])

How It Works

Logarithm is only defined for positive numbers. If your data might include zero or negatives, check with $gt before calling $log10.

Bonus — Decibel-Style Calculation

Many decibel formulas use base-10 logarithms. A simplified power ratio in dB:

mongosh
db.sensors.aggregate([
  {
    $project: {
      sensor: 1,
      powerRatio: 1,
      dB: {
        $multiply: [
          10,
          { $log10: "$powerRatio" }
        ]
      }
    }
  }
])

// powerRatio 100 → dB: 20  (10 × log10(100) = 10 × 2)

How It Works

Decibel calculations often multiply log₁₀ by 10. $log10 fits naturally into these formulas inside aggregation pipelines.

🚀 Use Cases

  • Scaling data — compress large values spanning many orders of magnitude onto a readable log scale.
  • Decibel calculations — acoustics, electronics, and signal processing often use base-10 logs.
  • Data transformation — normalize skewed distributions before statistical analysis.
  • Order of magnitude — bucket or label values by their power-of-10 range.

🧠 How $log10 Works

1

MongoDB evaluates the input

The expression resolves to a number from a field, literal, or nested operator.

Input
2

Checks for null

If the input is null, $log10 returns null.

Null
3

Computes base-10 log

MongoDB returns log₁₀(x) — the exponent that raises 10 to x.

Compute
=

Compressed scale

Large numbers become manageable log-scale values for analysis and visualization.

Conclusion

The $log10 operator is the simplest way to compute base-10 logarithms in MongoDB aggregation pipelines. Use it when your formulas need common logarithms — scaling data, decibel math, or order-of-magnitude bucketing.

It is equivalent to { $log: [ number, 10 ] } but easier to read. Guard against non-positive inputs with $cond, and use $ln or $log when you need a different base.

💡 Best Practices

✅ Do

  • Use $log10 when you always need base-10 logarithms
  • Remember powers of 10: 100 → 2, 1000 → 3, 10000 → 4
  • Guard with $cond when data may include zero or negatives
  • Pair with $floor for order-of-magnitude bucketing
  • Prefer $log10 over $log when base is fixed at 10

❌ Don’t

  • Apply $log10 to zero or negative numbers without guarding
  • Confuse $log10 with $ln (natural log, base e)
  • Use $log10 as a pipeline stage — it is an expression operator
  • Write { $log: [ x, 10 ] } when { $log10: x } is clearer
  • Expect meaningful results from non-numeric strings

Key Takeaways

Knowledge Unlocked

Five things to remember about $log10

Use these points when computing base-10 logarithms in MongoDB.

5
Core concepts
🔢 02

Simple Syntax

One argument.

Syntax
🛠 03

100 → 2

Powers of 10.

Math
🔄 04

= $log[,10]

Equivalent form.

Compare
📑 05

Positive Only

Guard invalid input.

Domain

❓ Frequently Asked Questions

$log10 returns the base-10 logarithm of a number. For example, log10(100) is 2 because 10² = 100. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $log10: <expression> }. The expression can be a field reference like "$value", a literal positive number, or another numeric expression.
$log10 is a shortcut for base-10 logarithm only. $log requires two arguments — number and base — so { $log10: "$value" } is equivalent to { $log: [ "$value", 10 ] }.
The input must be a positive number. log10(1) is 0. For null input, $log10 returns null. Zero and negative numbers are not valid for logarithm.
Use $log10 inside expression stages such as $project, $addFields, $set, and within other numeric expressions when scaling data, normalizing skewed values, or working with decibel-style calculations.

Continue the Operator Series

Move on to $lt for less-than comparisons, or review $log for custom bases.

Next: $lt →

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.

5 people found this page helpful