MongoDB $exp OperatorMongoDB 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:
Syntax:The exponent expression can be any valid expression that resolves to a number.
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 } Next TopicMongoDB $unwind operator |