MongoDB $abs Operator

MongoDB provides a variety of arithmetic expression operators. The $abs operator is one of those operators. The $abs operator is used to return the absolute value of a specific number in the code. Absolute value means that it always returns a positive value whether the number is positive or negative.

Syntax:

Important points:

  1. If the number is null, the $abs operator returns null.
  2. If the entered value refers to a missing field, the $abs operator returns null.
  3. If the number is NaN, the $abs operator returns NaN.

Examples:

In the following examples, we are working with

{
        {
                "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
                "std_name" : "Micky",
                "gender" : "Female",
                "class" : "X",
                "fees" : 5000,
                "exam_fees" : 500,
                "age" : 16,
                "Total_marks" : 405
                "Result" : "Pass"
        },
        {
                "_id" : ObjectId("56254d4fdf2222265r4g12ds34563"),
                "std_name" : "Moty",
                "gender" : "Male",
                "fees" : 4000,
                "exam_fees" : 500,
                "class" : "VII",
                "age" : 15,
                "Total_marks" : 705
                "Result" : "Pass"
        },
        {
                "_id" : ObjectId("56254d4fdf2222265r4g12ds31478"),
                "std_name" : "Thomas",
                "gender" : "Male",
                "fees" : 3000,
                "exam_fees" : 500,                                
                "class" : "V",
                "age" : 12,
                " Total_marks" : 450
                " Result" : "pass"
        },
        {
                "_id" : ObjectId("56254d4fdf2222265r4g12ds37832"),
                "std_name" : "Jin",
                "gender" : "Female",
                "fees" : 5000,
                "exam_fees" : 500,
                "class" : "X",
                "age" : 16,
                "Total_marks" : 750
                "Result" : "Pass"
        },
        { 
                "_id" : ObjectId("56254d4fdf2222265r4g12ds1c46"),
                "std_name" : "Mia",
                "gender" : "Female",
                "fees" : 6000,
                "exam_fees" : 500,
                "class" : "XI",
                "age" : 17,
                " Total_marks" : 450
                "Result" : "Pass"
       },
       {
                "_id" : ObjectId("56254d4fdf2222265r4g12ds315hj"),
                "std_name" : "Mike,
                "gender" : "Male",
                "fees" : {
                           "school_fees" :4000,
                           "exam_fees" : 500,
                           "pending_fees" : 950,
                         }
                "class" : "V",
                "age" : 15,
                "Total_marks" : 450
                "Result" : "Pass"
        }
}	

Example 1: Using $abs operator

In this example, we are going to find the total fees (fees + exam_fees) of female student only.

Output:

{
                "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
                "std_name" : "Micky",
                "class" : "X",
                "age" : 16,
                "Result" : "Pass",
                "Total_fees" : 5500
}
{
                "_id" : ObjectId("56254d4fdf2222265r4g12ds37832"),
                "std_name" : "Jin",
                "class" : "X",
                "age" : 16,
                "Result" : "Pass",
                "Total_fees" : 5500
}
{ 
                "_id" : ObjectId("56254d4fdf2222265r4g12ds1c46"),
                "std_name" : "Mia",
                "class" : "XI",
                "age" : 17,
                "Result" : "Pass",
                "Total_fees" : 6500,
}

Example 2: MongoDB $abs Operator (retrieving the data in the embedded document)

In this example, we are going to find the total fees (school_fees + exam_fees + pending_fees) of the student whose name is Mike.

Output:

{
  "_id" : ObjectId("56254d4fdf2222265r4g12ds315hj"),
  "std_name" : "Mike,
  "class" : "V",
  "age" : 15,
  "Result" : "Pass",
  "Total_fees" : 5450  
}





Latest Courses