$exp (聚合)
定义
行为
默认返回类型是 double
。如果至少有一个操作数是 decimal
,则返回类型是十进制。
如果参数解析为 null
或引用一个缺失的字段,则 $exp
返回 null
。如果参数解析为 NaN
,则 $exp
返回 NaN
。
示例 | 结果 |
---|---|
{ $exp: 0 } | 1 |
{ $exp: 2 } | 7.38905609893065 |
{ $exp: -2 } | 0.1353352832366127 |
示例
名为 accounts
的集合包含以下文档
db.accounts.insertMany( [ { _id: 1, interestRate: .08, presentValue: 10000 }, { _id: 2, interestRate: .0825, presentValue: 250000 }, { _id: 3, interestRate: .0425, presentValue: 1000 } ] )
以下示例计算连续复利的有效利率
db.accounts.aggregate( [ { $project: { effectiveRate: { $subtract: [ { $exp: "$interestRate"}, 1 ] } } } ] )
该操作返回以下结果
{ "_id" : 1, "effectiveRate" : 0.08328706767495864 } { "_id" : 2, "effectiveRate" : 0.08599867343905654 } { "_id" : 3, "effectiveRate" : 0.04341605637367807 }