MongoDB $abs operator
The $abs
operator in MongoDB is a fundamental arithmetic expression operator used in aggregation pipeline stages. Its primary function is to calculate the absolute value of a specified number. This operation ensures that only positive values are considered, regardless of the number’s sign, making it valuable for data cleaning and mathematical operations.
In this article, We will learn about the MongoDB $abs operator, explore its use cases, and walk us through practical examples.
What is the MongoDB $abs Operator?
MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and the $abs operator is one of them. This operator is used to find the absolute value of the specified number.
It can be used for data cleaning purposes to ensure that only positive values are considered, regardless of the original value's sign. It can be combined with other arithmetic operators like $add
, $subtract
, $multiply
and $divide
to perform complex mathematical operations.
Syntax:
{ $abs: <number> }
- number: This is a valid expression that resolves to a number. The
$abs
operator calculates the absolute value of this number.
Behavior:
- 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.
The $abs
operator is particularly helpful for data normalization, error handling, and ensuring that calculations are performed only with positive values.
Key Features of the MongoDB $abs
Operator
- Absolute Value Calculation: The operator ensures that all returned values are non-negative, irrespective of whether the input is positive or negative.
- Integration with Other Operators: The
$abs
operator can be combined with other arithmetic operators like$add
,$subtract
,$multiply
, and$divide
to perform more complex mathematical operations. - Data Cleaning: It can be used during data processing or cleaning to ensure the consistency of numeric values, particularly when negative values need to be converted into positive ones.
- Aggregation Framework: The
$abs
operator is part of MongoDB's aggregation framework, allowing it to be used in aggregation pipelines for advanced data manipulation.
Examples of MongoDB $abs operator
In the following examples, we will work with the GeeksforGeeks database and the Employee collection. The collection contains documents with employee details, including fields like firstSalary
, secondSalary
, and department
.
- Database: GeeksforGeeks
- Collection: Employee
- Document: three documents that contain the details of the employees in the form of field-value pairs.

Example 1: Using $abs for Total Salary Calculation
In this example, we are going to find the total salary of every employee in the development department, ensuring that we only use positive values (by taking the absolute value).
Query:
db.Employee.aggregate([{$match: {department: "Development"}},
... {$project: {name:1, tSalary: {$abs:
{$add: ["$firstSalary", "$secondSalary"]}}}}])
Output

Explanation:
- The query first filters employees in the "Development" department.
- Then, it calculates the total salary by adding the
firstSalary
andsecondSalary
fields. - The
$abs
operator is used to ensure that the result is a non-negative value.
Example 2: 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, where the salary is stored in an embedded document, and ensure that the result is non-negative.
Query:
db.Employee.aggregate([{$match: {department: "HR"}},
... {$project: {name: 1, tSalary: {$abs:
{$add: ["$salary.firstMonth",
"$salary.secondMonth",
"$salary.thirdMonth"]}}}}])
Output:

Explanation:
- The query filters employees in the "HR" department.
- It calculates the total salary for three months stored in the
salary
embedded document. - The
$abs
operator ensures that the total salary is always a positive value.
Conclusion
The MongoDB $abs
operator is a simple yet powerful tool that enables us to calculate the absolute value of a number directly within the MongoDB aggregation pipeline. This operator is essential for data cleaning, handling errors, and performing mathematical operations where negative values need to be disregarded. With its ability to work seamlessly with other arithmetic operators, the $abs
operator helps streamline data processing tasks and ensures consistent and accurate results in your MongoDB applications.