$ln (aggregation)

Definition

  • $ln

New in version 3.2.

Calculates the natural logarithm ln (i.e loge) of a number andreturns the result as a double.

$ln has the following syntax:

  1. { $ln: <number> }

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

$ln is equivalent to $log: [ <number>, Math.E ]expression, where Math.E is a JavaScript representation forEuler’s number e.

Behavior

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

ExampleResults
{ $ln: 1 }0
{ $ln: Math.E } where Math.E is a JavaScript representation for e.1
{ $ln: 10 }2.302585092994046

Example

A collection sales contains the following documents:

  1. { _id: 1, year: "2000", sales: 8700000 }
  2. { _id: 2, year: "2005", sales: 5000000 }
  3. { _id: 3, year: "2010", sales: 6250000 }

The following example transforms the sales data:

  1. db.sales.aggregate( [ { $project: { x: "$year", y: { $ln: "$sales" } } } ] )

The operation returns the following results:

  1. { "_id" : 1, "x" : "2000", "y" : 15.978833583624812 }
  2. { "_id" : 2, "x" : "2005", "y" : 15.424948470398375 }
  3. { "_id" : 3, "x" : "2010", "y" : 15.648092021712584 }

See also

$log