$pow
Introduced in 8.0
The $pow operator in Amazon DocumentDB allows you to raise a number to a power. This can be useful for performing exponential calculations within your aggregation pipeline.
Parameters
-
<number>(required): The number to be raised to a power. -
<exponent>(required): The power to which the number should be raised.
Example (MongoDB Shell)
The following example demonstrates how to use the $pow operator to calculate the square of a number.
Create sample documents
db.numbers.insertMany([ { "_id": 1, "value": 2 }, { "_id": 2, "value": 3 }, { "_id": 3, "value": 4 } ]);
Query example
db.numbers.aggregate([ { $addFields: { "square": { $pow: ["$value", 2] } } } ])
Output
[
{ "_id": 1, "value": 2, "square": 4 },
{ "_id": 2, "value": 3, "square": 9 },
{ "_id": 3, "value": 4, "square": 16 }
]
Code examples
To view a code example for using the $pow command, choose the tab for the language that you want to use: