MongoDB $pow Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $pow?

$pow is a MongoDB aggregation expression that raises a base to an exponent: baseexponent. Example: 5² = 25, written as { $pow: [ 5, 2 ] }.

Remember
$pow = Math.pow for aggregation

{ $pow: [ <number>, <exponent> ] }

{ $pow: [ 2, 3 ] }       →  8
{ $pow: [ "$side", 2 ] } →  side²

exactly two arguments (base, exponent)
0 ^ (negative) → pipeline error
null / missing → null
NaN → NaN

related: $sqrt for square root
NeedPrefer
Raise to a power$pow
Square root$sqrt
Product of numbers$multiply

How to Use $pow in MongoDB

Pass the base and exponent as a two-element array, then store the result in a stage.

  1. Choose an expression stage ($addFields or $project).
  2. Write { $pow: [ "$side", 2 ] } (base first, exponent second).
  3. Use integer exponents for clean squares/cubes; prefer $sqrt for square roots.
  4. Avoid 0 with a negative exponent—MongoDB will error.
PieceRequired?Role
Base (number)YesValue to raise
ExponentYesPower to apply
StageYes$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 }

Cheat Sheet

Keep these facts handy while computing powers.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $pow: [ "$n", 2 ] }
Squareexponent 2 → 5² = 25
Cubeexponent 3 → 5³ = 125
Any power{ $pow: [ 5, 0 ] } → 1
Forbidden0 to a negative exponent
Related$sqrt, $log, $exp

Live Preview

Watch $pow compute baseexponent for a few pairs.

Simulates { $pow: [ base, exponent ] } (same idea as the mongosh examples below).

Result
base     = 5
exponent = 2

$pow → 25

Check: 5 × 5 = 25 ✓

{ result: 25 }

Worked Walkthrough — Square a Side

A square has side: 5. Area is side².

Calculation
Document:
  { side: 5 }

$pow: [ "$side", 2 ]

5 ^ 2
  = 5 × 5
  = 25

Result:
  25  (area of the square)

Check: side × side = 25 ✓

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.

mongosh
db.shapes.insertOne({
  _id: 1,
  label: "Square",
  side: 5
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in shapes. Run find() to see it:

mongosh
db.shapes.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
label"Square"Shape name
side5Side length (base for $pow)
Picture
Square with side = 5:

  +---------+
  |         |
  |  side 5 |   area = side² = 5² = 25
  |         |
  +---------+

We will compute:
  side²  →  25   (area)
  side³  → 125   (cube)

How It Works

  1. Insert once. insertOne adds a single square document to shapes.
  2. MongoDB stores fields as written. label is text; side is the number 5.
  3. find() shows the same shape. What you inserted is what you get back—ready for $pow in the next examples.

Example 2 — Square (Area)

Goal: add an area field with side² using $pow.

mongosh
db.shapes.aggregate([
  {
    $addFields: {
      area: { $pow: [ "$side", 2 ] }
    }
  }
]);

How It Works

  1. Read the stored side. MongoDB already has side: 5 from Example 1.
  2. Set the exponent. The second array element 2 means square.
  3. Compute the power. $pow evaluates 5² = 25 (the area).
  4. Keep all fields. $addFields adds area next to the original fields—nothing is deleted.

Example 3 — Cube

Same side length, but raise to the third power: side³.

mongosh
db.shapes.aggregate([
  {
    $project: {
      side: 1,
      cube: { $pow: [ "$side", 3 ] }
    }
  }
]);

How It Works

  1. Same stored document. Still using side: 5 from Example 1.
  2. Exponent 3. 5³ = 5 × 5 × 5 = 125.
  3. Project a clean shape. $project keeps side and the cubed value.
  4. 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.

Performance Notes

ApproachCostBest for
$pow aloneCheap per documentAreas, volumes, scaling
Precompute on writeWrite cost, cheaper readsVery frequent power fields
Huge exponentsStill math, watch overflowValidate ranges for your data

Powers are inexpensive compared with large sorts or joins. Filter early with $match when only some documents need the field.

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.

Next: $radiansToDegrees

Convert an angle from radians to degrees in aggregation.

$radiansToDegrees tutorial →

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.

9 people found this page helpful