Confirm the number is non-negative and the base is greater than 1.
Use the result for scaling, bits math, or further expressions.
Piece
Required?
Role
Number
Yes
Value to take the log of (≥ 0)
Base
Yes
Log base (> 1)
Result
—
Double (or decimal if an operand is decimal)
Pseudocode
Pseudocode
for each document:
n = evaluate(numberExpression)
b = evaluate(baseExpression)
if n or b is null / missing:
result = null
else if n or b is NaN:
result = NaN
else:
result = log_b(n) // logarithm of n in base b
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while computing logarithms.
Watch $log compute logbase(number) for a few pairs.
Simulates { $log: [ number, base ] } (same idea as the mongosh examples below).
Result
number = 100
base = 10
$log → 2
Check: 10^2 = 100 ✓
{ result: 2 }
Trace
Worked Walkthrough — log10(100)
A reading stores value: 100. We want the base-10 logarithm.
Calculation
Document:
{ value: 100 }
$log: [ "$value", 10 ]
Ask: 10 to what power equals 100?
10^2 = 100
Result:
2
Check: pow(10, 2) = 100 ✓
Code
MongoDB Examples
We use one number document. First we store it, then we compute log10, then we use log2 to estimate bits needed. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one reading with value 100 and an integer 8 we will use for a log2 bits example.
Floor the result.$floor keeps the integer bit count (here already whole).
Check: binary of 8 is 1000 → 4 bits.
Edge Cases & Pitfalls
Check these before using $log in production pipelines.
domain
Base must be greater than 1
Bases like 1, 0, or negatives are invalid. Use a base such as 2 or 10.
domain
Number must be non-negative
Negative inputs are outside the domain. Prefer positive numbers for normal finite logs.
args
Order is [number, base]
{ $log: [ 10, 100 ] } is not the same as { $log: [ 100, 10 ] }. Number first, base second.
null
Null or missing → null
If either argument is null or missing, $log returns null, not an error.
shortcut
Prefer $log10 / $ln when fixed
For base 10 use $log10. For natural log use $ln. Keep $log when the base varies.
Analysis
Performance Notes
Approach
Cost
Best for
$log alone
Cheap per document
Scaling and log transforms
$log + $floor
Still lightweight
Bits / bucket estimates
Precompute on write
Write cost, cheaper reads
Very frequent log queries
Logarithms are inexpensive compared with large sorts or joins. Filter early with $match when only some documents need the log field.
Remember
Key Takeaways
Role: number + base → logarithm (double).
Syntax:{ $log: [ number, base ] }.
Domain: number ≥ 0, base > 1.
Shortcuts:$log10 and $ln for fixed bases.
One line:$log answers “base to what power equals this number?”—with a base you choose.
Frequently Asked Questions
$log returns the logarithm of a number in a base you choose. For example, { $log: [ 100, 10 ] } returns 2 because 10² = 100. Use it inside aggregation stages such as $project and $addFields.
The syntax is { $log: [ <number>, <base> ] }. Both arguments can be literals, field paths, or other numeric expressions. The result is typically a double.
The number must resolve to a non-negative value. The base must resolve to a positive number greater than 1. Null or missing arguments return null; NaN returns NaN.
$log lets you pick any valid base. $ln is natural log (base e). $log10 is a shortcut for base 10 — the same idea as { $log: [ number, 10 ] }.
Close, but JavaScript Math.log is natural log only. For a custom base in JS you use Math.log(n) / Math.log(base). MongoDB $log takes the base as the second argument.
Use $log inside expression stages such as $project, $addFields, and $set when scaling data, computing bits needed (log base 2), or normalizing values on a log scale.