MongoDB $multiply Operator

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

What You’ll Learn

The $multiply operator multiplies numbers together in MongoDB aggregation pipelines. It is a core arithmetic operator for line totals, tax amounts, scaled metrics, and any product-based calculation.

01

Multiplication

Product of values.

02

Array Syntax

Pass operands in an array.

03

Field Math

Multiply document fields.

04

3+ Operands

Chain many factors.

05

Use Cases

Totals, tax, scale.

06

Null Safety

Know how null inputs behave.

Definition and Usage

In MongoDB’s aggregation framework, the $multiply operator returns the product of numeric expressions. For example, { $multiply: [ "$price", "$quantity" ] } computes a line total, and { $multiply: [ 10, 3 ] } returns 30.

Like $add and $divide, $multiply always takes an array of at least two operands. You can multiply field values, literals, or nested expressions in a single call.

💡
Beginner Tip

Think of $multiply as MongoDB’s pipeline version of the * operator. Use it inside $project or $addFields — not as a standalone query filter.

📝 Syntax

The $multiply operator takes an array of two or more expressions:

mongosh
{ $multiply: [ <expression1>, <expression2>, ... ] }

Syntax Rules

  • $multiply — returns the product of all expressions in the array.
  • Array required — always wrap operands in square brackets, even for two values.
  • Operands can be field paths ("$price"), literals (1.08), or nested expressions.
  • Use inside stages like $project, $addFields, or $set.
  • If any operand is null, the result is null.
  • Multiplying by 0 returns 0; multiplying by 1 leaves the other value unchanged.

💡 $multiply vs $add vs $divide

$multiply[ a, b ] → a × b (product)
$add[ a, b ] → a + b (sum)
$divide[ a, b ] → a ÷ b (quotient; errors on divide by zero)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (arithmetic)
Syntax{ $multiply: [ expr1, expr2, ... ] }
Minimum operandsTwo expressions in an array
OutputNumeric product (or null)
Common stages$project, $addFields, $set
Literals
{ $multiply: [ 10, 3 ] }

Returns 30

Line total
{
  $multiply: [
    "$price",
    "$quantity"
  ]
}

price × quantity

Tax amount
{
  $multiply: [
    "$subtotal",
    0.08
  ]
}

8% tax

Null input
{
  $multiply: [ null, 5 ]
}

Returns null

Examples Gallery

Compute line totals, apply tax rates, multiply three dimensions for volume, and convert ratios to percentages.

📚 Order Line Items

Start with a lineItems collection and compute totals with $multiply.

Sample Input Documents

Suppose you have a lineItems collection with price and quantity:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "product": "Notebook", "price": 5,  "quantity": 3 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "product": "Pen",      "price": 2,  "quantity": 10 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "product": "Desk",     "price": 250, "quantity": 1 }
]

Example 1 — Basic $multiply for Line Total

Multiply price by quantity to get lineTotal:

mongosh
db.lineItems.aggregate([
  {
    $project: {
      product: 1,
      price: 1,
      quantity: 1,
      lineTotal: {
        $multiply: [ "$price", "$quantity" ]
      }
    }
  }
])

How It Works

  • Notebook: 5 × 3 = 15
  • Pen: 2 × 10 = 20
  • Desk: 250 × 1 = 250

📈 Practical Patterns

Tax calculations, multi-factor products, and percentage conversion.

Example 2 — Compute Tax on a Subtotal

Multiply subtotal by a tax rate (8% = 0.08):

mongosh
db.orders.aggregate([
  {
    $project: {
      orderId: 1,
      subtotal: 1,
      taxAmount: {
        $multiply: [ "$subtotal", 0.08 ]
      },
      grandTotal: {
        $add: [
          "$subtotal",
          { $multiply: [ "$subtotal", 0.08 ] }
        ]
      }
    }
  }
])

How It Works

For a subtotal of 100, tax is 8 and grand total is 108. Pair $multiply with $add for common invoice calculations.

Example 3 — Multiply Three Dimensions (Volume)

$multiply accepts more than two operands — useful for length × width × height:

mongosh
db.packages.aggregate([
  {
    $project: {
      sku: 1,
      volume: {
        $multiply: [
          "$length",
          "$width",
          "$height"
        ]
      }
    }
  }
])

How It Works

All operands are multiplied left to right. You can pass as many factors as needed in the array.

Example 4 — Convert Ratio to Percentage

After $divide, multiply by 100 for a display percentage:

mongosh
db.projects.aggregate([
  {
    $project: {
      name: 1,
      completionPct: {
        $multiply: [
          {
            $divide: [ "$completed", "$total" ]
          },
          100
        ]
      }
    }
  }
])

How It Works

If completed is 7 and total is 10, the ratio is 0.7 and $multiply by 100 gives 70 (70%).

Bonus — Apply a Discount Factor

Multiply price by quantity and a discount multiplier in one expression:

mongosh
db.lineItems.aggregate([
  {
    $project: {
      product: 1,
      finalTotal: {
        $multiply: [
          "$price",
          "$quantity",
          "$discountFactor"
        ]
      }
    }
  }
])

// price: 100, quantity: 2, discountFactor: 0.9
// finalTotal: 180 (10% off)

How It Works

A discountFactor of 0.9 means 10% off. Chain all factors in one $multiply instead of nesting multiple calls.

🚀 Use Cases

  • Line totals — compute price × quantity for invoices and carts.
  • Tax and fees — multiply subtotals by rate factors (0.08, 1.05, etc.).
  • Scaling values — convert units, currencies, or normalize scores by a factor.
  • Percentage display — multiply ratios from $divide by 100.

🧠 How $multiply Works

1

MongoDB evaluates operands

Each expression in the array is resolved per document — fields, literals, or nested expressions.

Input
2

$multiply computes the product

All values are multiplied together. If any operand is null, the result is null.

Compute
3

The result is stored

The product is written to the field you define in $project or $addFields.

Output
=

Computed totals ready

Use products for billing, metrics, and scaled analytics in your pipeline output.

Conclusion

The $multiply operator is essential for product-based math in MongoDB aggregation pipelines. From simple line totals to tax calculations and multi-factor volume formulas, it handles any multiplication you need.

Remember it always takes an array of at least two expressions, and null inputs produce null. Next in the series: $ne.

💡 Best Practices

✅ Do

  • Wrap operands in an array: { $multiply: [ a, b ] }
  • Use multiple operands for chained products (price × qty × discount)
  • Pair with $add for subtotal + tax patterns
  • Use $ifNull when fields may be missing
  • Multiply ratios by 100 after $divide for percentages

❌ Don’t

  • Omit the array brackets around operands
  • Use $multiply as a query filter outside expressions
  • Assume it converts non-numeric strings to numbers
  • Forget that any null operand returns null
  • Nest unnecessary $multiply calls when one array suffices

Key Takeaways

Knowledge Unlocked

Five things to remember about $multiply

Use these points when writing multiplication in aggregation pipelines.

5
Core concepts
📝 02

Array Syntax

[ expr1, expr2 ]

Syntax
🛠 03

2+ Operands

Chain many factors.

Usage
💰 04

Invoice Math

price × quantity.

Use case
05

Null Aware

null in → null out.

Edge case

❓ Frequently Asked Questions

$multiply multiplies two or more numeric expressions together in an aggregation pipeline. Use it inside stages like $project and $addFields to compute totals, scaled values, or products of fields.
The syntax is { $multiply: [ <expression1>, <expression2>, ... ] }. You must pass an array with at least two expressions — field references, literals, or nested expressions.
Yes. $multiply accepts an array of two or more expressions and returns the product of all of them. For example, { $multiply: [ "$length", "$width", "$height" ] } computes volume.
If any argument to $multiply is null, the result is null. Use $ifNull to provide a default before multiplying when fields may be missing.
$multiply returns the product of its operands. $add returns the sum. Both take an array of expressions and work inside aggregation pipeline stages.

Continue the Operator Series

Move on to $ne for not-equal comparisons, or review $add and $divide for more arithmetic.

Next: $ne →

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