On this page
$exp (aggregation)
在本页面
Definition
$exp
- 3.2 版中的新功能。
将欧拉数(即* e *)增大到指定的指数并返回结果。
$exp具有以下语法:
{ $exp: <exponent> }
<exponent>
表达式可以是任何有效的expression,只要它可以解析为数字。有关表达式的更多信息,请参见Expressions。
Behavior
如果参数解析为null
的值或引用了缺少的字段,则$exp
返回null
。如果参数解析为NaN
,则$exp
返回NaN
。
Example | Results |
---|---|
{ $exp: 0 } |
1 |
{ $exp: 2 } |
7.38905609893065 |
{ $exp: -2 } |
0.1353352832366127 |
Example
名为accounts
的集合包含以下文档:
{ _id: 1, rate: .08, pv: 10000 }
{ _id: 2, rate: .0825, pv: 250000 }
{ _id: 3, rate: .0425, pv: 1000 }
以下示例计算连续复利的有效利率:
db.accounts.aggregate( [ { $project: { effectiveRate: { $subtract: [ { $exp: "$rate"}, 1 ] } } } ] )
该操作返回以下结果:
{ "_id" : 1, "effectiveRate" : 0.08328706767495864 }
{ "_id" : 2, "effectiveRate" : 0.08599867343905654 }
{ "_id" : 3, "effectiveRate" : 0.04341605637367807 }