## How Does the $Abs Operator Work in MongoDB

The $abs operator gives an output of the absolute value in MongoDB. Now, we have some following example cases to demonstrate the function of the $abs operator in MongoDB. We are working with the “StudentReport” collection where the following documents are inserted which shows the student with the result report and details about their fee structure. The three documents in the “StudentReport” collection are inserted by utilizing the insertMany() command of MongoDB.

{

"_id": 1,

"Student": "Emily",

"Mid-termMarks": 300,

"Final-termMarks": 450,

"TotalMarks": "1000",

"StudentFees" : {"AdmissionFees": 50000,

"SemesterFees": 30000}

},

{

"_id": 2,

"Student": "Jenny",

"Mid-termMarks": 400,

"Final-termMarks": 490,

"TotalMarks": "1000",

"StudentFees" : {"AdmissionFees": 60000,

"SemesterFees": 35000}

},

{

"_id": 3,

"Student": "Lara",

"Mid-termMarks": 399,

"Final-termMarks": 500,

"TotalMarks": "1000",

"StudentFees" : {"AdmissionFees": 70000,

"SemesterFees": 50000}

}

])

There, we have an output after inserting the documents in the “StudentReport” collection. Now, the $abs operator can be applied to the documents’ fields by following this operator’s syntax:

## Example 1: Using the $Abs Operator

We use the $abs operator of MongoDB here to simply get the absolute value of the provided fields. The basic query of the $abs operator is given as follows:

{ $project: {

"Mid-termMarks": { $abs: [ "$Mid-termMarks" ] },

"Final-termMarks": { $abs: [ "$Final-termMarks" ] }}}

])

Here, we first deploy the aggregate method. Then, the $project is set. After that, we set the “Mid-termMarks” and “Final-termMarks” fields within the $project operator. Each field is assigned with the expression where the $abs operator is employed. The “Mid-termMarks” holds the “$abs: [ “$Mid-termMarks” ]” expression which gets the absolute value of the student mid-term marks from the $abs operator. Likewise, we apply the $abs operator to the second field which is “Final-termMarks”. Both these fields are set with the separate “$abs” operator to the individual absolute values that are obtained.

The absolute mid-term marks and the final-term marks are returned by the MongoDB $abs operator. Note that the absolute value from the $abs operator removes the negative sign if any field contains the negative sign in the document.

{ _id: 1, 'Mid-termMarks': 300, 'Final-termMarks': 450 },

{ _id: 2, 'Mid-termMarks': 400, 'Final-termMarks': 490 },

{ _id: 3, 'Mid-termMarks': 399, 'Final-termMarks': 500 }

]

## Example 2: Using the $Abs Operator with the $Add Operator

The $abs operator can also be used with another operator in MongoDB. We use the $add operator within the $abs operator to add the values together. We can apply the arithmetic operator inside the $abs operator as it only accepts the numerical value. The command of the $abs operator along with the $add operator for additional purposes is displayed as follows:

{$project: {

Marks: {$abs:

{$add: ["$Mid-termMarks", "$Final-termMarks"]}}}}])

Here, we first call the $project operator which is specified with the “Marks” field. The “Marks” field is assigned with the total marks which are returned by the $abs operator. Then, we provide the $abs operator expression – $abs: {$add: [“$Mid-termMarks”, “$Final-termMarks” – as we insert the “$add” operator which is further specified with the “$Mid-termMarks” and “$Final-termMarks” fields. The $add operator first adds the values of these set fields. Then, the additional results are assigned to the $abs operator. The $abs operator generates the absolute value in the output.

The result of the $abs operator by the $add operation is fetched here:

## Example 3: Using the $Abs Operator with the $Subtract Operator

We can use the $add operator in the $abs operator for the addition operation. We can also use the $subtract operator to evaluate the magnitude difference among the specified fields from the documents. Instead of the $add operator, we apply the $subtract operator which can be seen in the following:

{ $match: { _id: 1 } },

{ $project: {

"Mid-termMarks": 1,

"Final-termMarks": 1,

result: {

$abs: { $subtract: [ "$Mid-termMarks", "$Final-termMarks" ] }

}

}

}

]

)

Here, we match the document first using the $match operator. The $match operator searches the document whose “_id” field is “1”. Then, we set the $project operator where the “Mid-termMraks” and “final-termMarks” fields are assigned with a number “1” which indicates that these fields must be included in the output. After this, we have a “result” field which assigns the return value of the $abs operator. The $abs operator is applied where the $subtract operator is set to get the difference from the marks which are provided to the “Mid-termMarks” and the “final-termMarks”.

The previous $abs operator command of the “_id:1” document has the following output:

{ _id: 1, 'Mid-termMarks': 300, 'Final-termMarks': 450, result: 150 }

]

## Example 4: Using the $Abs Operator within the Embedded Document

Next, we set the $abs operator for the embedded document that functions the same as the previous example. Let’s consider another query of the $abs operator in the following demonstration:

{$match : {Student : "Lara"}},

{

$project :

{

Student : 1,

StudentFees : {$abs : {$add : [

"$StudentFees.AdmissionFees", "$StudentFees.SemesterFees" ] }}}}

])

Here, we match the document where the “Student” field has the “Lara” value. Then, we include the “Student” field in the output by assigning it with a value of “1” in the $project. The $project is set with the $abs operator on the embedded “StudentFees” field. The $abs operator gives the amount of the total fees by adding the values of the embedded fields – “AdmissionFees” and “SemesterFees”.

The following are the total fees that we get from the $abs operator:

## Example 5: Using the $Abs Operator for NaN Values

When the value that is provided to the $operator is not a numeric datatype or an undefined value, the “NaN” value is raised. We have a given example query where the $abs operator generates the resultant value as “NaN”:

{ $match: { _id: 3 } },

{ $project: {

"_id": 1,

"Mid-termMarks": { $abs: [ "$Mid-termMarks" ] },

"Final-termMarks": { $abs: [ 1 * "s" ] }

}

}

])

Here, we search for the document which contains the “id” value “3”. After searching, we deploy the $project operator where the “Mid-termMarks” is provided with the $abs operator to give the absolute value. Then, we set the $abs operator in the “Final-termMarks” where the $abs operator works on the numeric value of “1” which is multiplied by the string value of “s”.

The outcome displays the absolute value for the “Mid-termMarks” field and the NaN value for the “Final-termMarks”.

## Conclusion

The article is about the working of the $abs operator in MongoDB. We can determine the absolute value through the use of the $abs operator. The $abs operator along with its different example implementation is demonstrated in this article. We used the $abs operator with the embedded fields of the document. Then, we combined the different arithmetic operators within the $abs operator. Furthermore, the $abs operator takes the NaN argument values which returned the NaN result in the output.