![]() |
VOOZH | about |
MongoDB's $powoperator is a powerful tool within the aggregation framework which is designed to compute exponentiation operations directly on numeric fields. In this article, We will learn about the MongoDB $pow Operator in detail by understanding various examples and so on.
The MongoDB $pow operator is a mathematical aggregation operator used to calculate the exponentiation of a specified base to a specified exponent within a MongoDB aggregation pipeline.
Syntax:
{ $pow: [ <number>, <exponent> ] }Here, the number and exponent are valid expressions until it resolves to a number.
The result will be in the same type as the input, but it can be changed when it cannot be represented accurately in that type like in the following cases:
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: example
Document: two documents that contain the details of the shapes in the form of field-value pairs.
Output:
👁 ImageIn this example, we are going to find the area of square (i.e., (side)2 ) using a $pow operator. Here, the $pow operator contains two arguments, first one is the value of side field(i.e., 2) and the second one is the exponent(i.e., 2).
db.example.aggregate([
... {$match: {name: "Square"}},
... {$project: {area: {$pow: ["$side", 2]}}}])Output:
👁 ImageIn this example, we are going to find the value of (measurement.height +measurement.width)2using a $pow operator. Here, $pow operator contains two arguments, first one is the sum of the values of measurement.height and measurement.width fields and the second one is the exponent(i.e., 2).
db.example.aggregate([ {$match: {name: "Rectangle"}},
... {$project: {result:
... {$pow: [{$add:["$measurement.height", "$measurement.width"]}, 2]}}}])Output:
> db.example.aggregate([
{
$project: {
name: 1,
side: 1,
area: { $pow: ["$side", -2] } // Calculating area for squares using side length
}
}
]).pretty()Output
{
"_id": ObjectId("5f0b039892e6dfa3fc48de0a"),
"name": "Square",
"side": 4,
"area": 0.0625
}
Explanation:
$pow operator is used to calculate the area of a square with side length 4.-2 computes the reciprocal of the square of the side length, resulting in 0.0625.> db.example.aggregate([
{
$project: {
name: 1,
side: 1,
power: { $pow: ["$side", null] } // Using null exponent
}
}
]).pretty()Output:
{
"_id": ObjectId("5f0b039892e6dfa3fc48de0a"),
"name": "Square",
"side": 4,
"power": 1
}
{
"_id": ObjectId("5f0b03aa92e6dfa3fc48de0b"),
"name": "Rectangle",
"measurement": {
"height": 10,
"width": 15
},
"power": 1
}Explanation:
$pow operator with a null exponent results in 1 for any value of the base ($side in this case).power equal to 1, regardless of the value of side or measurement.> db.example.aggregate([
{
$project: {
name: 1,
side: 1,
area: { $pow: ["$length", 2] } // Using non-existent field `length`
}
}
]).pretty()Output:
{
"_id": ObjectId("5f0b039892e6dfa3fc48de0a"),
"name": "Square",
"side": 4,
"area": null
}
{
"_id": ObjectId("5f0b03aa92e6dfa3fc48de0b"),
"name": "Rectangle",
"measurement": {
"height": 10,
"width": 15
},
"area": null
}Explanation:
$length, the $pow operator returns null for each document.area as null because length field does not exist in the documents.Overall, MongoDB $pow operator calculates the exponentiation of a base value to a specified power within aggregation pipelines. It supports mathematical operations on numeric fields which allowing for efficient calculation of powers. This operator is useful for scenarios requiring computations like squared values or raising numbers to specific powers directly within MongoDB queries.