$exp (aggregation)
Definition
New in version 3.2.
Raises Euler’s number (i.e. e ) to the specified exponent andreturns the result.
$exp
has the following syntax:
- { $exp: <exponent> }
The <exponent>
expression can be any valid expression as long as it resolves to a number. Formore information on expressions, see Expressions.
Behavior
If the argument resolves to a value of null
or refers to a field that ismissing, $exp
returns null
. If the argument resolves toNaN
, $exp
returns NaN
.
Example | Results |
---|---|
{ $exp: 0 } | 1 |
{ $exp: 2 } | 7.38905609893065 |
{ $exp: -2 } | 0.1353352832366127 |
Example
A collection named accounts
contains the following documents:
- { _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 forcontinuous 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 }