$exp (arithmetic expression)

APPLIES TO: MongoDB vCore

The $exp operator raises Euler's number (e) to the specified exponent and returns the result. The mathematical constant e is approximately equal to 2.71828.

Syntax

The syntax for the $exp operator is as follows:

{ $exp: <exponent> }

Parameters

Description
<exponent> Any valid expression that resolves to a number.

Example

Let's understand the usage with sample data from the stores dataset to calculate exponential growth projections for sales.

db.stores.aggregate([
  { $match: { "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
  {
    $project: {
      name: 1,
      currentSales: "$sales.totalSales",
      projectedGrowth: {
        oneYear: {
          $multiply: [
            "$sales.totalSales",
            { $exp: 0.1 } // 10% growth rate
          ]
        },
        twoYears: {
          $multiply: [
            "$sales.totalSales",
            { $exp: 0.2 } // 20% growth rate
          ]
        }
      }
    }
  }
])

This will produce the following output:

{
  "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
  "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
  "currentSales": 151864,
  "projectedGrowth": {
    "oneYear": 167809.93,
    "twoYears": 185304.95
  }
}