Javatpoint Logo
Javatpoint Logo

MongoDB $pow Operator

What is the $pow operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $pow operator is one of those operators. This operator is used in the aggregation pipeline stages. This operator is used to find the exponent (power) of a number.

What is the exponent of a number?

The exponent (power) is the result of multiplying a number by itself. Typically, the exponent is represented in writing like this diagram.

MongoDB $pow Operator

Syntax of the $pow operator:

The < number > and < exponent > can be any valid expression until it resolves to a number.

Important point:

  1. If the number is null, the $pow operator returns null.
  2. If the number refers to a missing field, the $pow operator returns null.
  3. If the number is NaN, the $pow operator returns NaN.
S.No Example Output
1. { $pow: [ 4, 0 ] } 1
2. { $pow: [ 4, 2 ] } 16
3. { $pow: [ 4, -2 ] } 0.0625
4. { $pow: [ -4, 0.5 ] } NaN

Examples:

In the following examples, we are working with:

>db.shapes.find().pretty()
{
        {
         "_id" : 1, 
         "name" : "rectangle",
         "area" : 16
        }
        {
         "_id" : 2, 
         "name" : "square",
         "area" : 10
        }
        {
         "_id" : 3, 
         "name" : "circle",
         "perimeter" : 15,
         "area" : 10,
         "details" : { 
                           "radius" : 3,
                           "diameter" : 6
                          }
        }
        {
         "_id" : 4, 
         "name" : "rectangle",
         "area" : 0
        }
        {
         "_id" : 5, 
         "name" : "oval",
         "area" : 20
        }
        {
         "_id" : 6, 
         "name" : "triangle",
         "area" : 5
        }
        {
         "_id" : 7, 
         "name" : "rectangle",
         "area" : null
        }
}

Example 1: Using $pow operator

In this example, we are using the $pow operator to raise the "area" field in the shape of the rectangle by a specified exponent.

Output:

{
         "_id" : 1, 
         "name" : "rectangle",
         "area" : 16,
         "result" : 4096
}
{
         "_id" : 4, 
         "name" : "rectangle",
         "area" : 0,
         "result" : 0
}
{
         "_id" : 7, 
         "name" : "rectangle",
         "area" : null,
         "result" : null
}

In this example, we are using the area field as the base number and 3 as the exponent. Therefore, each "area" field of the rectangle has been raised by the power of 3.

Example 2: Negative exponent

If the base number is zero (0) and the exponent is negative, you cannot raise the number. In this case, it returns the error message.

Output:

uncaught exception : Error : command failed : {
	"ok" : 0,
	"errmsg" : "$pow cannot take a base of 0.and a negative exponent",
	"code" : 28764,
	"codeName" : "Location28764"4' [ ln;' lj; h
Lh;klh[pjlkh[pkoh[khp[o
} : aggregate failed:
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:618:17
assert.commandWorked@src/mongo/shell/assert.js:708:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046 : 12
@(shell) : 1 : 1

The error clearly states that."$pow cannot take a base of 0 and a negative exponent".

Output:

{
         "_id" : 5, 
         "name" : "oval",
         "area" : 20,
         "result" : 0.000125  
}

Example 3: Null Exponent

We have already seen in "example 1" that if the base number is null, the result also becomes null.

If the value of the exponent is null, it will still return null.

Output:

{
         "_id" : 6, 
         "name" : "triangle",
         "area" : 5,
         "result" : null  
}

Example 4: Non-existent fields

If the $pow operator is applied against a field that does not exist in the program, null is returned.

Output:

{
         "_id" : 2, 
         "name" : "square",
         "area" : 10,
         "result" : null  
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA