The $pow operator raises a number to an exponent in MongoDB aggregation pipelines. It computes baseexponent — useful for squaring dimensions, cubing edges, compound growth, and any power-based math.
01
Exponentiation
base^exponent.
02
Two Operands
Base and exponent.
03
Field Math
Square or cube fields.
04
Fractional Exp
Roots with 0.5.
05
Use Cases
Area, growth, scale.
06
vs $sqrt
Square root shortcut.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $pow operator returns base raised to the exponent. For example, { $pow: [ 2, 3 ] } returns 8 (2³), and { $pow: [ "$radius", 2 ] } squares the radius field.
Like $multiply and $divide, $pow is an aggregation expression operator. Use it inside $project, $addFields, or nested expressions — not as a standalone query filter.
💡
Beginner Tip
Think of $pow as MongoDB’s pipeline version of Math.pow(base, exponent). The first array element is always the base; the second is the exponent.
Foundation
📝 Syntax
The $pow operator takes an array of exactly two expressions:
mongosh
{ $pow: [ <number>, <exponent> ] }
Syntax Rules
First operand — the base (number to raise).
Second operand — the exponent (power).
Array required — always wrap both operands in square brackets.
Operands can be field paths ("$radius"), literals (2), or nested expressions.
Use inside stages like $project, $addFields, or $set.
If either operand is null, the result is null.
Exponent 0 returns 1 (except for 0^0, which MongoDB treats as 1).
💡 $pow vs $multiply vs $sqrt
$pow — [ base, exp ] → baseexp (e.g. 2³ = 8)
$multiply — [ a, b ] → a × b (product, not power)
Square a field — { $pow: [ "$x", 2 ] } or { $multiply: [ "$x", "$x" ] }
The base (1 + rate) is computed with $add, then raised to years with $pow, and multiplied by the principal.
Bonus — Square Root with Exponent 0.5
Use $pow with a fractional exponent, or prefer $sqrt for clarity:
mongosh
db.samples.aggregate([
{
$project: {
value: 1,
rootPow: { $pow: [ "$value", 0.5 ] },
rootSqrt: { $sqrt: "$value" }
}
}
])
// Both return the square root of value
How It Works
Raising to 0.5 is mathematically equivalent to a square root. For readability, use $sqrt when you only need a square root.
Applications
🚀 Use Cases
Geometry — compute area (side², r²) and volume (edge³) from dimensions.
Compound growth — model investment or population growth with (1 + rate)t.
Distance metrics — square differences before summing in Euclidean distance formulas.
Scientific scaling — apply power-law transformations to sensor or lab data.
🧠 How $pow Works
1
MongoDB evaluates base and exponent
Both expressions are resolved per document — fields, literals, or nested expressions.
Input
2
$pow computes baseexponent
MongoDB raises the base to the exponent. If either operand is null, the result is null.
Compute
3
The result is stored
The power is written to the field you define in $project or $addFields.
Output
=
📊
Power-based values ready
Use results for area, volume, growth models, and scaled analytics.
Wrap Up
Conclusion
The $pow operator raises numbers to an exponent in MongoDB aggregation pipelines. From squaring fields for area to modeling compound growth, it handles any baseexponent calculation with a simple two-operand syntax.
Remember it takes exactly two expressions (base and exponent), and null inputs produce null. Next in the series: $radiansToDegrees.
Use $sqrt for square roots when readability matters
Use $ifNull when base fields may be missing
❌ Don’t
Pass more than two operands (unlike $multiply)
Use $pow as a query filter outside expressions
Assume it converts non-numeric strings to numbers
Forget that negative base + fractional exponent can yield NaN
Swap base and exponent — order matters
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $pow
Use these points when writing exponentiation in aggregation pipelines.
5
Core concepts
📈01
Exponentiation
baseexponent.
Purpose
📝02
Two Operands
[ base, exp ]
Syntax
🛠03
Square / Cube
Exp 2 or 3.
Usage
🌎04
Geometry
Area and volume.
Use case
⚠05
Null Aware
null in → null out.
Edge case
❓ Frequently Asked Questions
$pow raises a number to a specified exponent in an aggregation pipeline. It computes base^exponent, such as 2^3 = 8 or $radius^2 for area calculations.
The syntax is { $pow: [ <number>, <exponent> ] }. The first expression is the base and the second is the exponent. Both can be field references, literals, or nested expressions.
$pow takes exactly two expressions in an array: the base and the exponent. Unlike $multiply, you cannot pass more than two operands to $pow.
Yes. Use { $pow: [ "$side", 2 ] } to square a value, or { $pow: [ "$edge", 3 ] } to cube it. For square roots, use $sqrt or $pow with an exponent of 0.5.
$pow raises one number to the power of another (base^exponent). $multiply returns the product of two or more numbers. To square with $multiply you would use { $multiply: [ "$x", "$x" ] }; $pow is cleaner for powers.