MongoDB $multiply Operator
In MongoDB, the $multiply
operator is a powerful tool used in aggregation pipelines to perform multiplication operations. This operator takes one or more expressions as arguments and multiplies them to produce a result.
In this article, we will explain the MongoDB $multiply
operator, its syntax, usage, and practical examples to help us master its functionality.
What is MongoDB $multiply Operator?
The MongoDB $multiply
operator is used to multiply two or more numeric values or expressions together. It returns the result of multiplying all provided values and can be used in the aggregation pipeline for advanced data manipulation and transformation. This operator is essential for performing arithmetic operations such as calculating totals, adjusting values, or performing mathematical computations directly within our MongoDB queries.
Syntax
{ $multiply: [ <expression1>, <expression2>, ... <expressionN> ] }
Parameters:
- Expression1, Expression2, ..., ExpressionN: These are the values or fields you want to multiply. They must resolve to numeric values.
- The
$multiply
operator operates on numeric expressions and will return an error if any expression does not resolve to a number.
Examples of MongoDB $multiply Operator
Here are some practical examples of using the $multiply
operator in MongoDB to perform calculations in the aggregation pipeline. In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: employee
- Document: three documents that contain the details of the employees in the form of field-value pairs.

Example 1: Multiply Using $multiply Operator
Here are some practical examples of using the $multiply
operator in MongoDB to perform calculations in the aggregation pipeline. In this example, we are going to multiply the salary of the development department's employees by 2.
Query:
db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, newSalary: {$multiply: ["$salary", 2]}}}])
Output:

Explanation:
- The query selects employees from the "Development" department.
- It then multiplies the
salary
field by 2 using the$multiply
operator. - The result is stored in the
newSalary
field.
Example 2: Multiply Using $multiply Operator in the Embedded Document
MongoDB allows us to use $multiply
on fields within embedded documents. This example multiplies the salary of employees in the "HR" department, where the salary is stored inside an embedded document.
Query:
db.employee.aggregate([{$match: {department: "HR"}},
... {$project: {name: 1, newSalary: {$multiply: ["$details.salary", 3]}}}])
Output:

Explanation:
- The
salary
field is embedded within thedetails
subdocument. - The
$multiply
operator multiplies the embeddedsalary
by 3, creating a newnewSalary
field.
Example 3: Add a Fixed Number to the Array with the Output
To add a fixed number to the array perfoPoint
for all documents in the employee
collection, we can use the $inc
operator. Here's how we can do it:
Query:
db.employee.updateMany({}, { $inc: { perfoPoint: 2.5 } })
Output:
{
"acknowledged": true,
"matchedCount": 3,
"modifiedCount": 3
}
How to Combine $multiply
with Other Operators
The $multiply
operator can be combined with other MongoDB operators, such as $add
, $divide
, or $subtract
, to perform more complex calculations. For example, we could calculate a bonus by multiplying a percentage increase with an employee's salary and then adding a fixed value.
Example combining $multiply
with $add
:
db.employee.aggregate([
{ $project: {
name: 1,
totalSalary: { $add: [{ $multiply: ["$salary", 1.1] }, 1000] }
} }
])
This query calculates a 10% salary increase and then adds a $1000 bonus.
Benefits of Using the $multiply
Operator in MongoDB
1. Perform Complex Calculations
The $multiply
operator allows you to perform arithmetic operations in MongoDB aggregation pipelines, saving time by eliminating the need for complex post-processing in your application.
2. Flexible Arithmetic Operations
You can multiply numeric fields, constants, or values from embedded documents, offering great flexibility in manipulating your data directly within MongoDB.
3. Efficient Data Transformations
The $multiply
operator is ideal for transforming data in real-time, whether adjusting salaries, calculating totals, or making adjustments to other numerical values.
Conclusion
The MongoDB $multiply
operator is an indispensable tool for performing numeric operations within the aggregation pipeline. Whether we’re multiplying field values, adjusting embedded documents, or performing complex calculations, this operator streamlines arithmetic operations directly within our queries. By using $multiply
, MongoDB developers can manipulate data efficiently, transforming raw data into actionable insights without additional processing steps.