Open In App

MongoDB $add Operator

Last Updated : 24 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The $add operator in MongoDB is a versatile arithmetic expression operator used to add numbers together or concatenate numbers and dates in the aggregation pipeline. This operator plays an important role in data manipulation and transformation. In this article, We will learn about the MongoDB $add Operator by understanding various examples in detail.

MongoDB $add

  • MongoDB $add operator is an arithmetic expression operator used to add numbers together or concatenate numbers and dates in the aggregation pipeline.

  • The primary use of the MongoDB $add the operator is to add numeric values together.
  • This operator can handle various data types, including integers, decimals, and dates, but all operands must be compatible for the operation to succeed.

Syntax: 

{ $add: [ <Expression1>, <Expression2>, ... <ExpressionN>] }

Here, the Expression must be a valid expression like numbers or a date.

Examples:

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.

Output:

Example 1: Add Numbers

In this example, we are going to find the total salary of every employee in the development department. 

db.Employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, tSalary: {$add:
["$firstSalary", "$secondSalary"]}}}])

Output:

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. 

db.Employee.aggregate([{$match: {department: "HR"}},
... {$project: {name: 1, tSalary: {$add: ["$salary.firstMonth",
"$salary.secondMonth",
"$salary.thirdMonth"]}}}])

Output:

Example 3: Adding date

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. 

db.Employee.aggregate([{$match: {department: "Testing"}},
... {$project: {extendprojectDate:
{$add: ["$projectEndDate", 5*24*60*60000]}}}])

Output:

Adding-date

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. By using the $add operator, developers can efficiently perform numeric and date arithmetic, enhancing the functionality and flexibility of their MongoDB applications.

FAQs on MongoDB $add

What data types can the $add operator handle?

The $add operator can handle integers, decimals, and dates. All operands must be compatible for the operation to succeed.

Can the $add operator be used to concatenate strings?

No, the $add operator is designed for numeric and date arithmetic operations. For string concatenation, you should use the $concat operator.

What happens if one of the operands in the $add operator is null?

If any operand in the $add operator is null, the result will be null.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: