- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Arithmetic Aggregation Operators >
- $abs (aggregation)
$abs (aggregation)¶
On this page
Definition¶
-
$abs¶ New in version 3.2.
Returns the absolute value of a number.
$abshas the following syntax:The
<number>expression can be any valid expression as long as it resolves to a number. For more information on expressions, see Expressions.
Behavior¶
If the argument resolves to a value of null or refers to a field that is
missing, $abs returns null. If the argument resolves to
NaN, $abs returns NaN.
| Example | Results |
|---|---|
{ $abs: -1 } |
1 |
{ $abs: 1 } |
1 |
{ $abs: null } |
null |
Example¶
A collection ratings contains the following documents:
The following example calculates the magnitude of difference between
the start and end ratings:
The operation returns the following results: