$floor (Floor)
Last updated: 2026-10-09
$floor (Floor)
$floor returns the largest integer less than or equal to a given number — rounding down. It is used to round amounts down, compute lower bounds, or bucket values, such as bucketing prices downward by 10-unit steps. A null or missing argument yields null and integers pass through unchanged. It is the opposite of $ceil: -1.2 becomes -2 with $floor but -1 with $ceil; it also differs from $trunc, which truncates toward zero so -1.2 becomes -1.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $floor: <numeric-expression> }
⚙️ Parameters
| numeric value | A numeric value or an expression resolving to one. |
|---|
Returns:The largest integer less than or equal to the input; null when null or missing
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $floor: round prices down to whole yuan
db.products.aggregate([
{ $project: {
priceFloor: { $floor: '$price' }
} }
])
Output:
{ '_id': 1, 'priceFloor': 9 }
{ '_id': 2, 'priceFloor': 12 }
📌 Notes
- The opposite of $ceil, with clear divergence on negatives
- Unlike $trunc: $floor rounds toward negative infinity while $trunc cuts toward zero
- Integer input passes through unchanged
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do $floor and $trunc differ on negatives?
A$floor rounds toward negative infinity (-1.2 becomes -2) while $trunc cuts toward zero (-1.2 becomes -1).
QWhat does $floor return for null?
AIt returns null without throwing.
QCan $floor keep decimal places?
ANo; use $round or $trunc to keep decimal places.