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.
Fundamentals
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.
Foundation
📝 Syntax
The $multiply operator takes an array of two or more expressions:
A discountFactor of 0.9 means 10% off. Chain all factors in one $multiply instead of nesting multiple calls.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $multiply
Use these points when writing multiplication in aggregation pipelines.
5
Core concepts
✖01
Product
a × b × c.
Purpose
📝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.