MongoDB $log10 Operator

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

What Is $log10?

$log10 is a MongoDB aggregation expression that returns the base-10 logarithm of a number. Example: log10(100) = 2 because 10² = 100. It is a shortcut for { $log: [ number, 10 ] }.

Remember
$log10 = logarithm base 10

{ $log10: <number> }

// same as:
{ $log: [ <number>, 10 ] }

number → non-negative
result → double (usually)

{ $log10: 1 }     →  0
{ $log10: 10 }    →  1
{ $log10: 100 }   →  2
{ $log10: 1000 }  →  3

null / missing → null
NaN → NaN
NeedPrefer
Base 10 only$log10
Any custom base$log
Natural log (base e)$ln

How to Use $log10 in MongoDB

Pass one number expression, then store the result in a stage.

  1. Choose an expression stage ($addFields or $project).
  2. Write { $log10: "$value" }.
  3. Confirm the number is non-negative.
  4. Use the result for scaling, orders of magnitude, or formulas like pH.
PieceRequired?Role
NumberYesValue to take log10 of (≥ 0)
Result—Double (or decimal if input is decimal)

Pseudocode

Pseudocode
for each document:
    n = evaluate(numberExpression)
    if n is null or missing:
        result = null
    else if n is NaN:
        result = NaN
    else:
        result = log10(n)   // base-10 logarithm
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while computing base-10 logs.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $log10: "$value" }
Equivalent{ $log: [ "$value", 10 ] }
DomainNon-negative number
Related$log, $ln, $pow
Sample checklog10(100) = 2

Live Preview

Watch $log10 return the base-10 logarithm for common powers of 10.

Simulates { $log10: number } (same idea as the mongosh examples below).

Result
number = 100

$log10 → 2

Check: 10^2 = 100 ✓

{ result: 2 }

Worked Walkthrough — log10(100)

A reading stores value: 100. We want the base-10 logarithm.

Calculation
Document:
  { value: 100 }

$log10: "$value"

Ask: 10 to what power equals 100?
  10^2 = 100

Result:
  2

Same as: { $log: [ 100, 10 ] } → 2 ✓

MongoDB Examples

We use one sample document. First we store it, then we compute $log10, then we use it in a simple pH-style formula. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one lab sample with value 100 and hydrogen-ion concentration H3O = 0.001.

mongosh
db.samples.insertOne({
  _id: 1,
  label: "Scale",
  value: 100,
  H3O: 0.001
});

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
value100Number for a plain log10
H3O0.001Concentration for pH-style math
Picture
100    --$log10-->  2        (10^2 = 100)
0.001  --$log10--> -3
                   then × -1 → pH 3

How It Works

  1. Insert once. insertOne adds a single document to samples.
  2. Store non-negative numbers. Both fields are valid $log10 inputs.
  3. find() shows the same shape. Ready for the log examples next.

Example 2 — Compute $log10

Goal: add a log10 field with log10(value).

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

How It Works

  1. Read the number. "$value" is 100.
  2. Apply $log10. Base is fixed at 10—no second argument needed.
  3. Get the exponent. 10² = 100, so the result is 2.
  4. Keep all fields. $addFields adds log10 next to value.

Example 3 — pH-Style Formula

Approximate pH as -log10(H3O) (same pattern as the MongoDB docs sample).

mongosh
db.samples.aggregate([
  {
    $project: {
      H3O: 1,
      pH: {
        $multiply: [
          -1,
          { $log10: "$H3O" }
        ]
      }
    }
  }
]);

How It Works

  1. Take log10 of concentration. $log10(0.001) = -3.
  2. Multiply by -1. $multiply flips the sign: -1 × -3 = 3.
  3. Read pH. For H3O = 0.001, pH is 3.
  4. Project a clean shape. Output shows concentration and pH only.

Edge Cases & Pitfalls

Check these before using $log10 in production pipelines.

domain

Number must be non-negative

Negative inputs are outside the domain. Prefer positive numbers for normal finite logs.

args

One argument only

Do not pass a base. For custom bases use $log with [ number, base ].

null

Null or missing → null

Missing fields return null, not an error. Handle them before further math if needed.

name

Not the same as $ln

$log10 is base 10. $ln is natural log (base e).

Performance Notes

ApproachCostBest for
$log10 aloneCheap per documentScaling and order-of-magnitude fields
$log10 + multiplyStill lightweightpH-style or signed log transforms
Precompute on writeWrite cost, cheaper readsVery frequent log10 queries

Base-10 log is inexpensive compared with large sorts or joins. Filter early with $match when only some documents need the field.

Key Takeaways

  • Role: number → base-10 logarithm (double).
  • Syntax: { $log10: "$value" }.
  • Same as: { $log: [ "$value", 10 ] }.
  • Domain: non-negative number.

One line: $log10 answers “10 to what power equals this number?”

Frequently Asked Questions

$log10 returns the base-10 logarithm of a number. For example, { $log10: 100 } returns 2 because 10² = 100. Use it inside aggregation stages such as $project and $addFields.
The syntax is { $log10: <number> }. The argument can be a field path like "$value", a literal, or another numeric expression. The result is typically a double.
$log10 is a shortcut for base 10 only. $log needs two arguments — number and base — so { $log10: "$value" } equals { $log: [ "$value", 10 ] }.
The input must resolve to a non-negative number. log10(1) is 0. Null or missing input returns null; NaN returns NaN. Prefer positive numbers for normal finite results.
Yes in spirit. Both compute base-10 log. In MongoDB you write { $log10: "$value" } inside an aggregation expression.
Use $log10 inside expression stages such as $project, $addFields, and $set when scaling data, normalizing skewed values, or computing pH-style formulas with -log10(concentration).

Next: $lt

Compare values with the less-than operator in queries and aggregation.

$lt 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