Use integer exponents for clean squares/cubes; prefer $sqrt for square roots.
Avoid 0 with a negative exponent—MongoDB will error.
Piece
Required?
Role
Base (number)
Yes
Value to raise
Exponent
Yes
Power to apply
Stage
Yes
$addFields / $project to store the result
Pseudocode
Pseudocode
for each document:
base = evaluate(numberExpression)
exp = evaluate(exponentExpression)
if base or exp is null / missing:
result = null
else if base is NaN or exp is NaN:
result = NaN
else if base == 0 and exp < 0:
error // aggregation fails
else:
result = base ^ exp
emit { ..., field: result }
Document:
{ side: 5 }
$pow: [ "$side", 2 ]
5 ^ 2
= 5 × 5
= 25
Result:
25 (area of the square)
Check: side × side = 25 ✓
Code
MongoDB Examples
We use one shape document. First we store it, then we square the side (area), then we cube it. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one square with side length 5. We save that length as one document in a collection named shapes.
Same stored document. Still using side: 5 from Example 1.
Exponent 3.5³ = 5 × 5 × 5 = 125.
Project a clean shape.$project keeps side and the cubed value.
Pattern. Change only the second array element to get other powers.
Edge Cases & Pitfalls
Check these before using $pow in production pipelines.
domain
No 0 to a negative power
MongoDB errors on { $pow: [ 0, -1 ] }. Guard zeros before negative exponents.
args
Order is [base, exponent]
{ $pow: [ 2, 5 ] } is 2⁵, not 5². Base first, exponent second.
null
Null or missing → null
If either argument is null or missing, $pow returns null—not an error.
root
Prefer $sqrt for square roots
Exponent 0.5 can work, but $sqrt is clearer. Negative base with a fractional exponent often yields NaN.
Analysis
Performance Notes
Approach
Cost
Best for
$pow alone
Cheap per document
Areas, volumes, scaling
Precompute on write
Write cost, cheaper reads
Very frequent power fields
Huge exponents
Still math, watch overflow
Validate ranges for your data
Powers are inexpensive compared with large sorts or joins. Filter early with $match when only some documents need the field.
Remember
Key Takeaways
Role: base + exponent → power result.
Syntax:{ $pow: [ number, exponent ] }.
Common: exponent 2 (square), 3 (cube).
Avoid:0 with a negative exponent.
One line:$pow answers “what is this number raised to that power?”—in aggregation, like Math.pow.
Frequently Asked Questions
$pow raises a number to a specified exponent and returns the result. For example, { $pow: [ 2, 3 ] } returns 8 because 2³ = 8. Use it inside aggregation stages such as $project and $addFields.
The syntax is { $pow: [ <number>, <exponent> ] }. The first argument is the base and the second is the exponent. Both can be literals, field paths, or other numeric expressions.
Use { $pow: [ "$side", 2 ] }. For a cube, use exponent 3. For a square root, prefer $sqrt, or use exponent 0.5 with care for negative bases.
$pow computes base^exponent. $multiply returns the product of two or more numbers. Squaring with $multiply would be { $multiply: [ "$x", "$x" ] }; $pow is clearer for powers.
No. MongoDB rejects raising 0 to a negative exponent. Other null or missing arguments return null; NaN returns NaN.
Yes in spirit. Both take a base and an exponent. In MongoDB you write { $pow: [ "$n", 2 ] } inside an aggregation expression.