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.
Fundamentals
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.
Foundation
📝 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
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (math)
Syntax
{ $log10: <expression> }
Base
10 (fixed)
Valid input
Positive numbers (and null → null)
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
Hands-On
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:
// Small (100) → logScale: 2, magnitude: 2
// Medium (1000) → logScale: 3, magnitude: 3
// Large (10000) → logScale: 4, magnitude: 4
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.
Decibel calculations often multiply log₁₀ by 10. $log10 fits naturally into these formulas inside aggregation pipelines.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $log10
Use these points when computing base-10 logarithms in MongoDB.
5
Core concepts
📝01
Base 10
Common logarithm.
Purpose
🔢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.