$exp (aggregation)

Definition

  • $exp

New in version 3.2.

Raises Euler’s number (i.e. e ) to the specified exponent andreturns the result.

$exp has the following syntax:

  1. { $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.

ExampleResults
{ $exp: 0 }1
{ $exp: 2 }7.38905609893065
{ $exp: -2 }0.1353352832366127

Example

A collection named accounts contains the following documents:

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

The following example calculates the effective interest rate forcontinuous compounding:

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

The operation returns the following results:

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