MongoDB $exp Operator

MongoDB provides a variety of logical query operators. The $exp operator is one of those operators. The $exe operator raises Euler's number (e) to a given exponent and returns the result of its operation.

What is Euler's number (e)?

The "e" number is also known as Euler's number. It is a mathematical constant that is approximately equal to 2.7182818284590452353602874713527. This number is only approximate because it is irrational. Euler's number is the basis of the natural logarithm.

Table of Euler's number values:

NumbersExponent values
01
12.718281828459045
27.38905609893065

Syntax:

The exponent expression can be any valid expression that resolves to a number.

  1. If the entered exponent argument value becomes null, then the $exp operator returns null.
  2. If the exponent argument refers to a missing field, then the $exp operator returns null.
  3. If the entered exponent argument value becomes NaN, then the $exp operator returns NaN.

Examples:

In the following examples, we are working with:

>db.example1.find().pretty()
{
        {
         "_id" : 1, 
         "name" : "circle",
         "area" : 9
        }
        {
         "_id" : 2, 
         "name" : "square",
         "area" : 1
        }
        {
         "_id" : 3, 
         "name" : "rectangle",
         "area" : 10,
         "unit" : { 
                           "height" : 2,
                           "width" : 3
                         }
        }
        {
         "_id" : 4, 
         "name" : "triangle",
         "area" : 5
        }
}

Example 1: Using $exp operator

In this example, we're applying the $exp operator to the "area" field.

Output:

{ "area" : 9, "result" : 8103.083927575384 }
{ "area" : 1, "result" : 2.718281828459045 }
{ "area" : 10, "result" : 22026.46579480672 }
{ "area" : 5, "result" : 148.4131591025766 }

Example 2: Using $exp operator in the embedded document

In this example, we are applying the $exp operator to the difference between the values of the height and width fields in the rectangle.

Output:

{ "-id" : 3, "result" : 0.367879441171442 }

Another examples:

In the following examples, we are working with:

>db.example1.find().pretty()
{
        {
         "_id" : 1, 
         "name" : "circle",
         "area" : null
        }
        {
         "_id" : 2, 
         "name" : "square",
         "data" : infinity
        }
        {
         "_id" : 3, 
         "name" : "rectangle",
         "data" : -infinity
        }
}

Example 3: Null value

In this example, we're applying the $exp operator to the "area" field.

Output:

{ "area" : null, "result" : null }

Here, the value of the "area" field is null, so it will return null.

Example 4: Missing fields

In this example, we're applying the $exp operator to the "perimeter" field.

Output:

{ "_id" : 1, "result" : null }
{ "_id" : 2, "result" : null }
{ "_id" : 3, "result" : null }

Example 5: Infinity values

Output:

{ "data" : Infinity, "result" : Infinity }
{ "data" : -Infinity, "result" : 0 }





Latest Courses