$abs is a MongoDB aggregation expression that returns the absolute value of a number—the magnitude without the sign. -10 becomes 10; 15 stays 15. Use it when you care about distance, not direction (refunds, temperature change, error size).
Remember
$abs = Math.abs for aggregation
{ $abs: <number expression> }
negative → positive
positive / zero → unchanged
null / missing → null
NaN → NaN
Common stages: $project, $addFields, $set
Pass a numeric expression to $abs (field path or nested operator).
Name the output field (e.g. delta or absoluteAmount).
For differences, nest $subtract inside $abs.
Piece
Required?
Role
$abs
Yes
Returns absolute value of its argument
Expression
Yes
Number, "$field", or nested expression
Stage
Yes
Usually $project / $addFields / $set
Pseudocode
Pseudocode
for each document:
value = evaluate(expression)
if value is null or missing:
result = null
else if value is NaN:
result = NaN
else if value < 0:
result = -value
else:
result = value
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing absolute-value expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $abs: <expression> }
Difference pattern
{ $abs: { $subtract: ["$a", "$b"] } }
null / missing
Returns null
NaN
Returns NaN
Sample check
50→80 delta = 30; 90→70 delta = 20
Try it
Live Preview
Watch $abs turn signed temperature changes into magnitudes. Sample: 50→80, 40→40, 90→70, 60→70.
Simulates $abs: { $subtract: ["$startTemp", "$endTemp"] } (same idea as the mongosh examples below).
One reading drops from 90 to 70. Subtraction alone can go negative; $abs keeps the change size.
Calculation
Document:
{ _id: 3, startTemp: 90, endTemp: 70 }
$subtract: [ "$startTemp", "$endTemp" ]
90 - 70 → 20
$abs of that difference
$abs(20) → 20
Another case (_id: 1):
startTemp 50, endTemp 80
50 - 80 → -30
$abs(-30) → 30
Output field: delta = how far the temperature moved
Code
MongoDB Examples
We use a tiny temperature collection for magnitude of change, then a transactions list for absolute amounts. Run these in mongosh after inserting each sample once.
Example 1 — Sample Data
Imagine four temperature readings. Each one has a start temperature and an end temperature. Some go up, some go down. We save them in a collection named temperatureChange.
What each reading means:
_id 1: 50 ──► 80 went up
_id 2: 40 ──► 40 no change
_id 3: 90 ──► 70 went down
_id 4: 60 ──► 70 went up
Next lesson step (Example 2):
how far did it move? (ignore up vs down)
_id 1: |50 − 80| = 30
_id 2: |40 − 40| = 0
_id 3: |90 − 70| = 20
_id 4: |60 − 70| = 10
How It Works
Insert once.insertMany adds four temperature documents to temperatureChange.
MongoDB stores fields as written. Each document has _id, startTemp, and endTemp—plain numbers.
find() shows the same shape. What you inserted is what you get back—ready for $abs in the next example.
Example 2 — Absolute Difference Between Two Fields
Goal: for each reading, report how far the temperature moved, ignoring whether it rose or fell.
One line:$abs keeps the size of a number and drops the sign—ideal for distances, deltas, and magnitude reports.
Frequently Asked Questions
$abs returns the absolute value of a number. Negative values become positive; positive values and zero stay the same. Use it inside aggregation expression stages such as $project and $addFields.
Write { $abs: <expression> }. The expression can be a field path like "$amount", a literal number, or another numeric expression such as $subtract.
$abs returns null. It does not throw an error. If the argument is NaN, $abs returns NaN.
Inside expression stages: $project, $addFields, $set, and inside other expressions (for example nested in $group field expressions). It is not a query filter operator like $eq.
Combine $subtract with $abs: { $abs: { $subtract: [ "$a", "$b" ] } }. That gives how far apart two numbers are, ignoring which is larger.
Yes for numbers. Think of $abs as MongoDB’s aggregation version of Math.abs—run it in the pipeline, not as a find() filter by itself.