MongoDB $pow Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Arithmetic

What You’ll Learn

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.

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.

📝 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" ] }
$sqrt — dedicated square root; or { $pow: [ "$x", 0.5 ] }

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (arithmetic)
Syntax{ $pow: [ base, exponent ] }
Operand countExactly two expressions
OutputNumeric result (or null)
Common stages$project, $addFields, $set
Literals
{ $pow: [ 2, 3 ] }

Returns 8

Square
{
  $pow: [
    "$side",
    2
  ]
}

side²

Cube
{
  $pow: [
    "$edge",
    3
  ]
}

edge³

Null input
{
  $pow: [ null, 2 ]
}

Returns null

Examples Gallery

Square dimensions for area, cube edges for volume, compute circle area with πr², and model compound growth.

📚 Basic Exponentiation

Start with literal values, then apply $pow to document fields.

Example 1 — Basic $pow with Literals

Raise 2 to the power of 3:

mongosh
db.samples.aggregate([
  {
    $project: {
      result: { $pow: [ 2, 3 ] }
    }
  }
])

// result: 8

How It Works

MongoDB evaluates as 2 × 2 × 2 = 8. The first array element is the base; the second is the exponent.

Sample Input Documents

Suppose you have a shapes collection with dimension fields:

mongosh
[
  { "_id": 1, "name": "Square A",  "side": 5,  "radius": null },
  { "_id": 2, "name": "Square B",  "side": 10, "radius": null },
  { "_id": 3, "name": "Circle C",  "side": null, "radius": 3 },
  { "_id": 4, "name": "Cube D",    "edge": 4 }
]

Example 2 — Square a Field (Area of a Square)

Compute area as side²:

mongosh
db.shapes.aggregate([
  {
    $match: { side: { $ne: null } }
  },
  {
    $project: {
      name: 1,
      side: 1,
      area: { $pow: [ "$side", 2 ] }
    }
  }
])

How It Works

  • Square A: 5² = 25
  • Square B: 10² = 100

📈 Practical Patterns

Circle area, cube volume, compound growth, and square roots.

Example 3 — Circle Area (π × r²)

Combine $pow with $multiply for πr²:

mongosh
db.shapes.aggregate([
  {
    $match: { radius: { $ne: null } }
  },
  {
    $project: {
      name: 1,
      radius: 1,
      area: {
        $multiply: [
          3.14159,
          { $pow: [ "$radius", 2 ] }
        ]
      }
    }
  }
])

How It Works

For radius 3, $pow gives 9, then $multiply by π gives approximately 28.27. Nest $pow inside other arithmetic operators as needed.

Example 4 — Cube Volume (edge³)

Raise the edge length to the third power:

mongosh
db.shapes.aggregate([
  {
    $project: {
      name: 1,
      edge: 1,
      volume: { $pow: [ "$edge", 3 ] }
    }
  }
])

// edge: 4 → volume: 64

How It Works

4³ = 4 × 4 × 4 = 64. Use exponent 3 for cubic volume and 2 for area of squares or circles.

Bonus — Compound Growth

Model future value with (1 + rate)^years:

mongosh
db.investments.aggregate([
  {
    $project: {
      principal: 1,
      rate: 1,
      years: 1,
      futureValue: {
        $multiply: [
          "$principal",
          {
            $pow: [
              { $add: [ 1, "$rate" ] },
              "$years"
            ]
          }
        ]
      }
    }
  }
])

// principal: 1000, rate: 0.05, years: 3
// futureValue: 1000 × 1.05³ ≈ 1157.63

How It Works

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.

🚀 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.

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.

💡 Best Practices

✅ Do

  • Pass exactly two operands: { $pow: [ base, exponent ] }
  • Use $pow for squaring (2) and cubing (3)
  • Pair with $multiply for formulas like πr²
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $pow

Use these points when writing exponentiation in aggregation pipelines.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $radiansToDegrees for angle conversion, or review $multiply for more arithmetic.

Next: $radiansToDegrees →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful