MongoDB $add Operator
The $add
operator in MongoDB is a versatile and essential tool within the aggregation framework. It enables us to perform arithmetic operations like addition on numeric values, as well as concatenate dates and numbers. Whether we are aggregating data or manipulating documents, the $add
operator is indispensable for transforming your data in meaningful ways.
In this article, We will learn about the MongoDB $add Operator by understanding various examples in detail, exploring its syntax, and use cases, to demonstrate how it can be applied effectively in MongoDB aggregation pipelines.
What is the MongoDB $add Operator?
MongoDB $add
operator is an arithmetic expression operator used to add numbers together or concatenate numbers and dates in the aggregation pipeline. This operator can handle various data types, including integers, decimals, and dates, but all operands must be compatible for the operation to succeed.
This makes $add
a flexible and powerful operator for data manipulation and transformation in MongoDB. The $add
operator in MongoDB is an arithmetic expression operator used to perform addition. It can be used to:
- Add numeric values together.
- Concatenate numbers and dates in the aggregation pipeline.
Syntax:
{ $add: [ <Expression1>, <Expression2>, ... <ExpressionN>] }
Parameters
<Expression1>, <Expression2>, ..., <ExpressionN>
are the expressions to be added.- These expressions can be fields, constants, or other expressions that evaluate to compatible data types (e.g., numbers or dates).
Examples of $add
Operator
Let's look at some practical examples where the $add
operator can be used in MongoDB aggregation pipelines. In the following examples, we are working with:
- Database: GeeksforGeeks
- Collection: Employee
- Document: four documents that contain the details of the employees in the form of field-value pairs.

Example 1: Add Numbers Together
In this example, we are going to calculate the total salary of every employee in the development department by adding their firstSalary
and secondSalary
fields.
Query:
db.Employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, tSalary: {$add:
["$firstSalary", "$secondSalary"]}}}])
Output:

Explanation:
- We first match the documents where the department is "Development."
- Then, we use
$project
to display the employee's name and calculate their total salary by addingfirstSalary
andsecondSalary
.
Example 2: Adding numbers in the embedded document
In this example, we are going to find a total of three months' salary of the employee in the HR department by adding three months' worth of salary, which is stored in an embedded document.
Query:
db.Employee.aggregate([{$match: {department: "HR"}},
... {$project: {name: 1, tSalary: {$add: ["$salary.firstMonth",
"$salary.secondMonth",
"$salary.thirdMonth"]}}}])
Output:

Explanation:
- We match employees in the "HR" department.
- The
salary
field contains embedded documents for the three months, and we use the$add
operator to sum up these values to get the total salary for the three months.
Example 3: Adding to a Date Field
In this example, we are going to extend the last date of the project by adding 5 days (i.e, 5*24*60*60000) in the projectEndDate field of the testing department. The date addition is done by adding 5 days (converted to milliseconds) to the existing projectEndDate
Query:
db.Employee.aggregate([{$match: {department: "Testing"}},
... {$project: {extendprojectDate:
{$add: ["$projectEndDate", 5*24*60*60000]}}}])
Output:

Explanation:
- We first match the documents in the "Testing" department.
- Then, we add 5 days (in milliseconds) to the
projectEndDate
using the$add
operator.
Conclusion
The $add
operator in MongoDB is an essential tool for performing arithmetic operations within the aggregation pipeline. Its ability to handle various data types and support complex calculations makes it indispensable for data analysis and manipulation tasks. Understanding how to leverage this operator effectively can enhance our ability to perform complex calculations and date manipulations in MongoDB. By using the $add
operator, developers can efficiently perform numeric and date arithmetic, enhancing the functionality and flexibility of their MongoDB applications.