$ln (aggregation)
On this page本页内容
Definition定义
$ln-
Calculates the natural logarithm ln (i.e log e) of a number and returns the result as a double.计算一个数字的自然对数ln(即log e),并以双精度返回结果。$lnhas 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.有关表达式的详细信息,请参阅表达式。$lnis equivalent to相当于$log: [ <number>, Math.E ]expression, whereMath.Eis 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将返回null。If the argument resolves to 如果参数解析为NaN, $ln returns NaN.NaN,$ln将返回NaN。
{ $ln: 1 } | 0 |
{ $ln: Math.E }Math.E is a JavaScript representation for e.Math.E是e的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 }