MongoDB $floor Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $floor?

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

Remember
$floor = Math.floor for aggregation

{ $floor: <expression> }

 23.8 → 23
 23.1 → 23
 -3.2 → -4   // toward −∞
 10   → 10

null / missing → null
NaN → NaN
UsePrefer
Round down (toward −∞)$floor
Round up (toward +∞)$ceil
Chop toward zero$trunc

How to Use $floor in MongoDB

Pass a number (or an expression that produces one) and emit the floored integer.

  1. Choose an expression stage ($addFields or $project).
  2. Pass a field, literal, or nested math expression to $floor.
  3. Remember negatives go toward −∞ (-3.2 → -4).
  4. Use $ceil, $trunc, or $round when you need a different rounding rule.
PieceRequired?Role
$floorYesRound down to integer
ExpressionYesNumber, 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 }

Cheat Sheet

Keep these facts handy while rounding down in pipelines.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $floor: <expression> }
DirectionToward negative infinity
vs $ceilOpposite direction (up)
vs $trunc$trunc toward zero on negatives
IntegersUnchanged
Sample check$floor(23.8) → 23

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 }

Worked Walkthrough — Whole Dollars

A notebook costs 23.8. We want the whole-dollar amount by rounding down.

Calculation
Document:
  { item: "Notebook", price: 23.8 }

Number line (positive):
  ... 22  23  23.8  24 ...
           ^
         floor

$floor(23.8) → 23 ✓

Negative check:
  $floor(-3.2) → -4  (not -3)

MongoDB Examples

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.

mongosh
db.products.insertOne({
  _id: 1,
  item: "Notebook",
  price: 23.8,
  adjustment: -3.2
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in products. Run find() to see it:

mongosh
db.products.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
item"Notebook"Product name
price23.8Decimal price (Example 2)
adjustment-3.2Negative number (Example 3)
Picture
price 23.8
  floor → 23   (drop the .8)

adjustment -3.2
  floor → -4   (toward −∞, not -3)

How It Works

  1. Insert once. insertOne adds a single product document to products.
  2. MongoDB stores numbers as written. price and adjustment keep their decimals.
  3. find() shows the same shape. Ready for $floor in the next examples.

Example 2 — Floor a Price

Goal: compute wholeDollars as $floor of price.

mongosh
db.products.aggregate([
  {
    $addFields: {
      wholeDollars: {
        $floor: "$price"
      }
    }
  }
]);

How It Works

  1. Read the stored price. MongoDB already has price: 23.8 from Example 1.
  2. Round down. $floor drops the fractional part toward the lower integer → 23.
  3. Keep all fields. $addFields adds wholeDollars without deleting price.

Example 3 — Floor a Negative

Same document, but floor adjustment to see the −∞ rule clearly.

mongosh
db.products.aggregate([
  {
    $addFields: {
      flooredAdj: {
        $floor: "$adjustment"
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using adjustment: -3.2 from Example 1.
  2. Move toward −∞. The greatest integer ≤ −3.2 is -4, not -3.
  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.

Performance Notes

ApproachCostBest for
$floor alonePer-document mathWhole units from decimals
Math then $floorStill O(docs)Floored totals after $divide / $multiply
Floor then groupBucket by whole numbersHistogram-style ranges

Flooring is cheap compared with large sorts or joins. Filter early with $match when only some documents need rounding.

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.

Next: $function

Run custom JavaScript functions inside an aggregation pipeline.

$function tutorial →

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.

9 people found this page helpful