$log10 (aggregation)

Definition

  • $log10

New in version 3.2.

Calculates the log base 10 of a number and returns the result as adouble.

$log10 has the following syntax:

  1. { $log10: <number> }

The <number> expression can be any valid expression as long as it resolves to a non-negativenumber. For more information on expressions, seeExpressions.

$log10 is equivalent to $log: [ <number>, 10 ] expression.

Behavior

If the argument resolves to a value of null or refers to a field that ismissing, $log10 returns null. If the argument resolves toNaN, $log10 returns NaN.

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

Example

A collection samples contains the following documents:

  1. { _id: 1, H3O: 0.0025 }
  2. { _id: 2, H3O: 0.001 }
  3. { _id: 3, H3O: 0.02 }

The following example calculates the pH value of the samples:

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

The operation returns the following results:

  1. { "_id" : 1, "pH" : 2.6020599913279625 }
  2. { "_id" : 2, "pH" : 3 }
  3. { "_id" : 3, "pH" : 1.6989700043360187 }

See also

$log