MongoDB $multiply Operator (original) (raw)

Last Updated : 5 May, 2026

The $multiply operator multiplies two or more numeric values or expressions within MongoDB aggregation pipelines to compute results directly in the database.

Syntax

{ $multiply: [ , , ... ] }

Examples of MongoDB $multiply Operator

In the following examples, we are working with:

Screenshot-2026-02-11-100032

**Example 1: Using $multiply Operator

Multiply the salary of the development department's employees by 2.

db.employee.aggregate([
{ $match: { department: "Development" } },
{
$project: {
name: 1,
newSalary: { multiply:["multiply: ["multiply:["salary", 2] }
}
}
])

**Output:

multiply

Example 2: Multiply Using $multiply Operator in the Embedded Document

Multiplies the salary of employees in the "HR" department, where the salary is stored inside an embedded document.

db.employee.aggregate([
{ $match: { department: "HR" } },
{
$project: {
name: 1,
newSalary: { multiply:["multiply: ["multiply:["details.salary", 3] }
}
}
])

**Output:

multiply2

Combine $multiply with Other Operators

$multiply can be combined with other arithmetic operators in aggregation pipelines to build complex calculations like bonuses or adjusted totals directly in MongoDB.

**Example combining multiplywithmultiply with multiplywithadd

Given below is the sample data:

Screenshot-2026-02-11-103114

**Query:

db.employee.aggregate([
{
$project: {
name: 1,
totalPay: {
$add: [
"$salary",
{ multiply:["multiply: ["multiply:["salary", "$bonusRate"] },
1000
]
}
}
}
])

**Output:

multiplyandAdd

Benefits of Using the $multiply Operator in MongoDB

Here are some benefits:

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.