Database Manual / Reference / Query Language / Expressions

$log10 (expression operator)(表达式运算符)

Definition定义

$log10

Calculates the log base 10 of a number and returns the result as a double.计算一个数字的对数基数10,并将结果作为双精度数返回。

$log10 has the following syntax:具有以下语法:

{ $log10: <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.有关表达式的详细信息,请参阅表达式

$log10 is equivalent to $log: [ <number>, 10 ] expression.相当于$log: [ <number>, 10 ]表达式。

Behavior行为

The default return type is a double. If at least one operand is a decimal, then the return type is a decimal.默认返回类型是double。如果至少有一个操作数是decimal,则返回类型是decimal

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

Example示例Results结果
{ $log10: 1 }0
{ $log10: 10 }1
{ $log10: 100 }2
{ $log10: 1000 }3

Example示例

Create a collection named samples with the following documents:使用以下文档创建一个名为samples的集合:

db.samples.insertMany(
[
{ _id: 1, H3O: 0.0025 },
{ _id: 2, H3O: 0.001 },
{ _id: 3, H3O: 0.02 }
]
)

The following example calculates the pH value of the samples:以下示例计算样品的pH值:

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

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

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

Tip

$log