The $log operator calculates the logarithm of a number in a specified base. Unlike $ln (which always uses base e), $log lets you choose base 10, base 2, or any other valid base.
01
Custom Base
Pick any valid base.
02
Syntax
[ number, base ].
03
Base 10 & 2
Common log scales.
04
vs $ln
Natural vs custom.
05
Use Cases
Scaling, normalization.
06
Domain Rules
Positive number & base.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $log operator computes logbase(number) — the power you must raise the base to in order to get the number. For example, log₁₀(100) = 2 because 10² = 100, and log₂(8) = 3 because 2³ = 8.
Use $log when your formula requires a specific logarithm base. For natural logarithms (base e), use $ln instead. For base 10 only, you can also use the dedicated $log10 operator.
💡
Beginner Tip
$log takes two arguments in an array: the number first, then the base. Both can be field references or literals. The base must be positive and cannot be 1.
Foundation
📝 Syntax
The $log operator takes a two-element array: number and base.
mongosh
{ $log: [ <number>, <base> ] }
Common Patterns
mongosh
// Log base 10 of a field
{ $log: [ "$quantity", 10 ] }
// Log base 2 of a literal
{ $log: [ 8, 2 ] }
// Both arguments from fields
{ $log: [ "$value", "$logBase" ] }
Syntax Rules
Array form — $log requires [ number, base ], not an object.
<number> — must be a positive number (or null → null).
<base> — must be positive and cannot equal 1.
logb(1) = 0 — for any valid base, the log of 1 is zero.
Use inside stages like $project, $addFields, or $set.
For base e, prefer $ln; for base 10, consider $log10.
💡 $log vs $ln vs $log10
{ $log: [ 100, 10 ] } → 2 (base 10)
{ $log: [ 8, 2 ] } → 3 (base 2)
{ $ln: 10 } → ≈ 2.302585 (base e)
{ $log10: 100 } → 2 (same as base-10 $log)
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (math)
Syntax
{ $log: [ <number>, <base> ] }
Arguments
Number (positive), then base (positive, ≠ 1)
Output
Logarithm of number in the given base (or null)
Common stages
$project, $addFields, $set
Base 10
{
$log: [
"$qty", 10
]
}
log₁₀(qty)
Base 2
{
$log: [ 8, 2 ]
}
Returns 3
log(1)
{
$log: [ 1, 10 ]
}
Returns 0
Null
{
$log: [ null, 10 ]
}
Returns null
Hands-On
Examples Gallery
Walk through an orders collection and compute logarithms with different bases using $project.
📚 Order Quantities
Start with an orders collection and compute base-10 logarithms of order quantities.
Sample Input Documents
Suppose you have an orders collection with a quantity field:
The second array element is the base. It must be positive and not equal to 1 for every document where you apply $log.
Applications
🚀 Use Cases
Scaling data — compress values that span many orders of magnitude onto a readable log scale.
Normalization — transform skewed distributions before analysis or machine learning.
Performance metrics — convert exponential growth curves into linear trends for comparison.
Custom bases — compute log base 2, base 4, or any domain-specific base in one expression.
🧠 How $log Works
1
MongoDB reads two arguments
The array [ number, base ] is evaluated from fields, literals, or nested expressions.
Input
2
Validates the domain
Number must be positive; base must be positive and not 1. null input returns null.
Check
3
Computes the logarithm
MongoDB returns logbase(number) — the exponent that raises the base to the number.
Compute
=
📊
Log-scaled value
Your pipeline gets the logarithm in the base you chose, ready for further analysis.
Wrap Up
Conclusion
The $log operator gives you flexible logarithm calculations with any valid base inside MongoDB aggregation pipelines. Remember the array syntax { $log: [ number, base ] } and the domain rules for positive inputs.
For natural logs use $ln; for base 10 specifically, $log10 is a convenient shortcut. Choose the operator that matches your formula and keep invalid inputs guarded with $cond when data quality varies.
Ensure the number is positive and the base is positive and ≠ 1
Use $log10 when you only need base-10 logarithms
Guard with $cond when data may include zero or negatives
Compare results with $ln to verify you chose the right base
❌ Don’t
Use object syntax like { $log: { expr: base } } — it is invalid
Omit the base argument — use $ln for natural log instead
Pass base 1 — logarithm base 1 is undefined
Confuse base-10 output with natural log results
Use $log as a pipeline stage — it is an expression operator
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $log
Use these points when computing logarithms in MongoDB.
5
Core concepts
📝01
Custom Base
[ number, base ].
Purpose
🔢02
Array Syntax
Not an object.
Syntax
🛠03
log(100)=2
Base 10 example.
Math
🔄04
vs $ln
Different bases.
Compare
📑05
Positive Only
Guard invalid input.
Domain
❓ Frequently Asked Questions
$log returns the logarithm of a number in a specified base. It takes two arguments: the number and the base. For example, log base 10 of 100 is 2.
The syntax is { $log: [ <number>, <base> ] }. Both arguments can be field references, literals, or other numeric expressions. The base must be a positive number not equal to 1.
$log lets you choose any valid base. $ln is natural logarithm (base e). $log10 is a shortcut for base-10 logarithm — equivalent to { $log: [ number, 10 ] }.
The number must be positive. The base must be positive and cannot be 1. If either argument is null, $log returns null.
Use $log inside expression stages such as $project, $addFields, $set, and within other numeric expressions when scaling, normalizing, or analyzing data on a log scale.