MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $abs operator is one of them. This operator is used to find the absolute value of the specified number.
Syntax:
{ $abs: <number> }
Here, the number is a valid expression until it resolves to a number.
- If the entered value is null, then this operator will return null.
- If the entered value is NaN, then this operator will return NaN.
- If the entered value is a missing field, then this operator will return null.
Examples:
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.

Using $abs operator:
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: {$abs:
{$add: ["$firstSalary", "$secondSalary"]}}}}])

Using $abs operator in embedded documents:
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: {$abs:
{$add: ["$salary.firstMonth",
"$salary.secondMonth",
"$salary.thirdMonth"]}}}}])

Recommended Posts:
- MongoDB - Multiply Operator ($mul)
- MongoDB - Rename Operator ($rename)
- MongoDB - Current Date Operator ($currentDate)
- MongoDB - SetOnInsert Operator ($setOnInsert)
- MongoDB - Positional Operator ($)
- MongoDB - $pull Operator
- MongoDB - $push Operator
- MongoDB - $pullAll Operator
- MongoDB - $pop Operator
- MongoDB - $addToSet Operator
- MongoDB - All Positional Operator ($[])
- MongoDB $ln Operator
- MongoDB $floor Operator
- MongoDB $divide Operator
- MongoDB $add Operator
- MongoDB $ceil Operator
- MongoDB $sqrt Operator
- MongoDB $multiply Operator
- MongoDB $mod Operator
- MongoDB $log10 Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

