MongoDB $floor Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Math

What You’ll Learn

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.

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.

📝 Syntax

The $floor operator takes one numeric expression:

mongosh
{ $floor: <expression> }

Key Values

mongosh
$floor( 23.8 )  → 23
$floor( 23.0 )  → 23
$floor( -3.2 )  → -4    // toward negative infinity
$floor( null )  → null

Syntax Rules

  • $floor — returns the largest integer less than or equal to the input.
  • <expression> — can be a field path ("$temperature"), a literal number, or another numeric expression.
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
⚠️
$floor vs $trunc vs $ceil

$floor rounds toward negative infinity (-3.7-4). $trunc removes decimals toward zero (-3.7-3). $ceil rounds toward positive infinity (-3.2-3). Pick the operator that matches your business rule.

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation math expression operator
Syntax{ $floor: <expression> }
InputNumber or numeric expression
OutputInteger (or null)
Common stages$project, $addFields, $set
Positive
{
  $floor: 23.8
}

Returns 23

Field
{
  $floor: "$temperature"
}

Round field down

Negative
{
  $floor: -3.2
}

Returns -4

Null input
{
  $floor: null
}

Returns null

Examples Gallery

Walk through sample data, an aggregation pipeline, and the output step by step.

🌡️ Temperature Data

Start with a temperatures collection and round decimal readings down with $project.

Sample Input Documents

Suppose you have a temperatures collection with location and temperature fields:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "location": "New York", "temperature": 23.5 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "location": "Los Angeles", "temperature": 31.8 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "location": "Chicago", "temperature": 17.2 }
]

Example 1 — Round Temperature Down to Integer

Use $project to add a roundedTemperature field:

mongosh
db.temperatures.aggregate([
  {
    $project: {
      location: 1,
      temperature: 1,
      roundedTemperature: { $floor: "$temperature" }
    }
  }
])

How It Works

  • New York: 23.523 (decimal dropped).
  • Los Angeles: 31.831.
  • Chicago: 17.217.
  • Whole numbers like 20.0 stay unchanged at 20.

📈 Practical Patterns

Handle negative values, combine with arithmetic, and normalize data for reporting.

Example 2 — Negative Numbers Round Toward −∞

$floor behaves differently from $trunc on negative values:

mongosh
db.readings.aggregate([
  {
    $project: {
      value: 1,
      floored: { $floor: "$value" },
      truncated: { $trunc: "$value" }
    }
  }
])

// value: -3.7  → floored: -4, truncated: -3
// value:  4.9  → floored: 4,  truncated: 4

How It Works

$floor always moves toward negative infinity. This matters for financial adjustments, altitude readings below sea level, or any signed decimal data.

Example 3 — Whole Units After Division

Calculate how many full boxes fit using $divide and then floor the result:

mongosh
db.inventory.aggregate([
  {
    $project: {
      item: 1,
      totalQty: 1,
      boxSize: 1,
      fullBoxes: {
        $floor: {
          $divide: [ "$totalQty", "$boxSize" ]
        }
      }
    }
  }
])

How It Works

$divide returns a decimal (e.g. 3.916...). $floor keeps only complete units — leftover items do not count as a full box.

Example 4 — $floor with a Literal Expression

You can pass a literal number or nested expression directly:

mongosh
db.samples.aggregate([
  {
    $project: {
      fromLiteral: { $floor: 9.99 },
      fromAdd: {
        $floor: {
          $add: [ "$basePrice", "$tax" ]
        }
      }
    }
  }
])

How It Works

{ $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.

🚀 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.923, -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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $floor

Use these points when rounding numbers in aggregation pipelines.

5
Core concepts
📝 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.

Explore More MongoDB Operators

Continue with $function for custom JavaScript, or review $ceil for rounding up.

Next: $function →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful