$ln (aggregation)

On this page本页内容

Definition定义

$ln

New in version 3.2.版本3.2中的新功能。

Calculates the natural logarithm ln (i.e loge) of a number and returns the result as a double.计算一个数字的自然对数ln(即loge),并以双精度返回结果。

$ln has the following syntax:语法如下所示:

{ $ln: <number> }

The <number> expression can be any valid expression as long as it resolves to a non-negative number. <number>表达式可以是任何有效的表达式,只要它解析为非负数。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

$ln is equivalent to $log: [ <number>, Math.E ] expression, where Math.E is a JavaScript representation for Euler’s number e.相当于$log: [ <number>, Math.E ]表达式,其中Math.E是欧拉的数字e的JavaScript表示。

Behavior行为

If the argument resolves to a value of null or refers to a field that is missing, $ln returns null. 如果参数解析为null值或引用缺少的字段,$ln返回nullIf the argument resolves to NaN, $ln returns NaN.如果参数解析为NaN$ln返回NaN

Example示例Results结果
{ $ln: 1 } 0
{ $ln: Math.E } where Math.E is a JavaScript representation for e.其中Math.Ee的JavaScript表示。 1
{ $ln: 10  } 2.302585092994046

Example示例

A collection sales contains the following documents:sales集合包含以下文档:

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

The following example transforms the sales data:以下示例转换sales数据:

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

The operation returns the following results:操作返回以下结果:

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

See also参阅

$log