$floor is a MongoDB aggregation expression that rounds a number down to the nearest integer—toward negative infinity. Positive decimals drop the fraction; negative decimals move further left on the number line.
Pass a field, literal, or nested math expression to $floor.
Remember negatives go toward −∞ (-3.2 → -4).
Use $ceil, $trunc, or $round when you need a different rounding rule.
Piece
Required?
Role
$floor
Yes
Round down to integer
Expression
Yes
Number, field, or nested math
Pseudocode
Pseudocode
for each document:
x = evaluate(expression)
if x is null or missing:
result = null
else if x is NaN:
result = NaN
else:
result = greatest integer ≤ x
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while rounding down in pipelines.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $floor: <expression> }
Direction
Toward negative infinity
vs $ceil
Opposite direction (up)
vs $trunc
$trunc toward zero on negatives
Integers
Unchanged
Sample check
$floor(23.8) → 23
Try it
Live Preview
Watch $floor round values down. Try a negative number to see −∞ behavior.
Simulates { $floor: x } (same idea as the mongosh examples below).
Result
x = 23.8
$floor(x) → 23
{ floored: 23 }
Trace
Worked Walkthrough — Whole Dollars
A notebook costs 23.8. We want the whole-dollar amount by rounding down.
We use one product with a price and a negative adjustment. First floor the price, then floor the negative value. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one product priced at 23.8 with an adjustment of -3.2. We save both numbers in a collection named products.
Same stored document. Still using adjustment: -3.2 from Example 1.
Move toward −∞. The greatest integer ≤ −3.2 is -4, not -3.
Compare siblings.$trunc(-3.2) would be -3; $floor is stricter downward.
Edge Cases & Pitfalls
Check these before using $floor in production pipelines.
neg
Negatives are not “drop decimals”
$floor(-3.2) is -4. If you want toward-zero chopping, use $trunc.
null
Null and missing become null
Missing fields return null, not zero. Handle them before flooring if nulls are invalid.
vs ceil
Need round up? Use $ceil
$ceil moves toward +∞. $floor(23.1) is 23; $ceil(23.1) is 24.
type
Non-numeric values error
Strings and other non-numbers make the pipeline fail. Ensure the expression is numeric first.
Analysis
Performance Notes
Approach
Cost
Best for
$floor alone
Per-document math
Whole units from decimals
Math then $floor
Still O(docs)
Floored totals after $divide / $multiply
Floor then group
Bucket by whole numbers
Histogram-style ranges
Flooring is cheap compared with large sorts or joins. Filter early with $match when only some documents need rounding.
Remember
Key Takeaways
Role: round down to the nearest integer.
Direction: toward negative infinity.
Negatives:-3.2 → -4 (not -3).
Siblings:$ceil up, $trunc toward zero.
One line:$floor answers “what is the greatest integer ≤ this number?”
Frequently Asked Questions
$floor rounds a number down to the nearest integer toward negative infinity. For example, 23.8 becomes 23 and -3.2 becomes -4. Use it inside aggregation stages such as $project and $addFields.
Use { $floor: <expression> }. The expression can be a field path, a literal number, or nested math.
$floor rounds toward −∞. $ceil rounds toward +∞. $trunc removes the fractional part toward zero, so -3.7 becomes -3 with $trunc but -4 with $floor.
$floor returns null when the input is null or missing. If the argument is NaN, $floor returns NaN.
No. Whole numbers stay the same. $floor(10) is 10.
Yes for the math. Think of $floor as MongoDB’s aggregation version of Math.floor—run it in the pipeline, not as a standalone find() filter.