MongoDB $multiply Operator
Last Updated :
21 Jun, 2024
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 learn about the MongoDB $multiply Operator in detail.
MongoDB $multiply Operator
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and the $multiply operator is one of them.
This operator is used to multiply one number by another number and return the result.
Syntax of $multiply Operator:
{ $multiply: [ <expression1>, <expression2>, ... <expressionN> ] }
Here, in this operator, the arguments are passed in an array. The expression must be a valid expression until it resolves to a number.
Examples of MongoDB $multiply Operator
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
In this example, we are going to multiply the salary of the development department’s employees by 2.
db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, newSalary: {$multiply: ["$salary", 2]}}}])
Output:

Example 2: Multiply using $multiply operator in the embedded document
In this example, we are going to multiply the salary of the HR department’s employees by 3.
db.employee.aggregate([{$match: {department: "HR"}},
... {$project: {name: 1, newSalary: {$multiply: ["$details.salary", 3]}}}])
Output:
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, you can use the $inc operator. Here’s how we can do it:
db.employee.updateMany({}, { $inc: { perfoPoint: 2.5 } })
Output:
{
"acknowledged": true,
"matchedCount": 3,
"modifiedCount": 3
}
Conclusion
The $multiply operator in MongoDB is a versatile tool for performing multiplication operations within aggregation pipelines. Whether you’re multiplying values directly or from embedded documents, this operator simplifies complex calculations and helps you to get the meaningful insights from your data.
FAQs on MongoDB $multiply Operator
Is it possible to combine the $multiply operator with other arithmetic operators in MongoDB?
Yes, the $multiply operator can be used in conjunction with other arithmetic operators like $add, $subtract, and $divide to perform complex calculations in MongoDB aggregation pipelines.
What happens if one of the expressions in the $multiply operator resolves to a non-numeric value?
If one of the expressions in the $multiply operator resolves to a non-numeric value, MongoDB will return an error. It is important to ensure that all expressions passed to the operator result in numeric values.
Is it possible to use the $multiply operator to update documents in a collection directly?
No, the $multiply operator is designed for use in aggregation pipelines and cannot be used directly to update documents in a collection. To update documents, you would typically use the $set operator in an update operation.
Please Login to comment...