$abs
The $abs operator in Amazon DocumentDB returns the absolute value of a number. It can be used in the aggregation pipeline to perform mathematical operations on numeric fields.
Parameters
-
number: The numeric expression for which the absolute value will be returned.
Example (MongoDB Shell)
This example demonstrates the usage of the $abs operator to find the absolute value of a numeric field.
Create sample documents
db.numbers.insertMany([ { "_id": 1, "value": -5 }, { "_id": 2, "value": 10 }, { "_id": 3, "value": -3.14 }, { "_id": 4, "value": 0 } ]);
Query example
db.numbers.aggregate([ { $project: { "_id": 1, "absolute_value": { $abs: "$value" } }} ]);
Output
[
{ "_id": 1, "absolute_value": 5 },
{ "_id": 2, "absolute_value": 10 },
{ "_id": 3, "absolute_value": 3.14 },
{ "_id": 4, "absolute_value": 0 }
]
Code examples
To view a code example for using the $abs command, choose the tab for the language that you want to use: