$exp (aggregation)

On this page本页内容

Definition定义

$exp

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

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行为

If the argument resolves to a value of null or refers to a field that is missing, $exp returns null. 如果参数解析为null值或引用缺少的字段,$exp返回nullIf the argument resolves to NaN, $exp returns NaN.如果参数解析为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的集合包含以下文档:

{ _id: 1, rate: .08, pv: 10000 }
{ _id: 2, rate: .0825, pv: 250000 }
{ _id: 3, rate: .0425, pv: 1000 }

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

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

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

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