Database Manual / Reference / Query Language / Expressions

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

Definition定义

$exp

Raises Euler's number (i.e. e ) to the specified exponent and returns the result.将欧拉数(即e)幂乘到指定的指数并返回结果。

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

{ $exp: <exponent> }

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

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, $exp returns null. If the argument resolves to NaN, $exp returns NaN.如果参数解析为null值或引用缺少的字段,$exp将返回null。如果参数解析为NaN$exp将返回NaN

Example示例Results结果
{ $exp: 0 }1
{ $exp: 2 }7.38905609893065
{ $exp: -2 }0.1353352832366127

Example示例

A collection named accounts contains the following documents:名为accounts的集合包含以下文档:

db.accounts.insertMany( [
{ _id: 1, interestRate: .08, presentValue: 10000 },
{ _id: 2, interestRate: .0825, presentValue: 250000 },
{ _id: 3, interestRate: .0425, presentValue: 1000 }
] )

The following example calculates the effective interest rate for continuous compounding:以下示例计算连续复利的实际利率:

db.accounts.aggregate( [ { $project: { effectiveRate: { $subtract: [ { $exp: "$interestRate"}, 1 ] } } } ] )

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

{ "_id" : 1, "effectiveRate" : 0.08328706767495864 }
{ "_id" : 2, "effectiveRate" : 0.08599867343905654 }
{ "_id" : 3, "effectiveRate" : 0.04341605637367807 }