The $floor operator rounds a number down to the nearest integer in MongoDB aggregation pipelines. It is MongoDB’s equivalent of JavaScript’s Math.floor() — useful when you need whole numbers from decimal data.
01
Round Down
Drop the decimal part.
02
Syntax
One expression inside $floor.
03
$project Stage
Add rounded fields easily.
04
Negatives
-3.2 becomes -4, not -3.
05
Use Cases
Temperatures, pricing, tiers.
06
vs $ceil / $trunc
Know the rounding direction.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $floor operator rounds a numeric expression down to the nearest integer. For example, 23.8 becomes 23, and -3.2 becomes -4 (rounding toward negative infinity). This is useful for temperature displays, whole-unit pricing, age brackets, and any scenario where you need the largest integer less than or equal to a value.
💡
Beginner Tip
Think of $floor as MongoDB’s version of JavaScript’s Math.floor(). Use it inside aggregation expression stages like $project, not as a standalone query filter operator.
{ $floor: 9.99 } returns 9. When applied to an expression like { $add: [ "$basePrice", "$tax" ] }, it floors the computed total — useful for display pricing or integer-based billing tiers.
Applications
🚀 Use Cases
Data normalization — convert decimal readings to whole numbers for consistent reporting.
Statistical summaries — bucket temperatures, ratings, or scores into integer ranges.
Inventory and packaging — count full boxes, pallets, or batches after division.
Visualization — simplify decimal values for charts and dashboards that display integers.
🧠 How $floor Works
1
MongoDB reads the expression
The pipeline evaluates the input — a field like "$temperature" or a numeric expression.
Input
2
$floor rounds toward −∞
Decimals are dropped in the downward direction. 23.9 → 23, -3.1 → -4.
Transform
3
The integer result is stored
The floored value is written to the field you define in $project or $addFields.
Output
=
📊
Whole-number data
You get integer values ready for grouping, display, and integer-based business rules.
Wrap Up
Conclusion
The $floor operator is a practical math tool in MongoDB aggregation pipelines. It converts decimal numbers to integers by rounding down, which is especially helpful for temperature data, inventory calculations, and normalized reporting.
For beginners, the key idea is simple: wrap any numeric expression in { $floor: ... } inside a stage like $project, and MongoDB returns the largest integer less than or equal to that value.
Use $floor when you need the largest integer ≤ the value
Combine it with $divide for whole-unit calculations
Keep original decimal fields when precision still matters
Test with positive, negative, and zero values
Compare with $ceil and $trunc before choosing
❌ Don’t
Confuse $floor with $round (nearest) or $trunc (toward zero)
Use $floor as a query filter operator outside expressions
Assume it rounds to decimal places — it returns integers only
Forget that null input returns null
Apply it to non-numeric strings expecting conversion
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $floor
Use these points when rounding numbers in aggregation pipelines.
5
Core concepts
🔢01
Round Down
Largest integer ≤ value.
Purpose
📝02
Simple Syntax
{ $floor: expr }
Syntax
🛠03
Pipeline Stages
$project, $addFields, $set.
Usage
🌡️04
Whole Numbers
Temps, units, tiers.
Use case
⚠05
Negatives Differ
-3.2 → -4, not -3.
Edge case
❓ 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. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $floor: <expression> }. The expression can be a field reference like "$temperature", a literal number, or another numeric expression.
$floor always rounds toward negative infinity. $ceil rounds toward positive infinity (up). $trunc removes the decimal part toward zero, so -3.7 becomes -3 while $floor makes it -4.
$floor returns null when the input is null. It does not throw an error.
Use $floor inside expression stages such as $project, $addFields, and $set when you need whole-number results from decimal values.