$floor (aggregation)

Definition

  • $floor

New in version 3.2.

Returns the largest integer less than or equal to the specifiednumber.

$floor has the following syntax:

  1. { $floor: <number> }

The <number> expression can be any valid expression as long as it resolves to a number. Formore information on expressions, see Expressions.

Behavior

If the argument resolves to a value of null or refers to a field that ismissing, $floor returns null. If the argument resolves toNaN, $floor returns NaN.

ExampleResults
{ $floor: 1 }1
{ $floor: 7.80 }7
{ $floor: -2.8 }-3

Example

A collection named samples contains the following documents:

  1. { _id: 1, value: 9.25 }
  2. { _id: 2, value: 8.73 }
  3. { _id: 3, value: 4.32 }
  4. { _id: 4, value: -5.34 }

The following example returns both the original value and the floorvalue:

  1. db.samples.aggregate([
  2. { $project: { value: 1, floorValue: { $floor: "$value" } } }
  3. ])

The operation returns the following results:

  1. { "_id" : 1, "value" : 9.25, "floorValue" : 9 }
  2. { "_id" : 2, "value" : 8.73, "floorValue" : 8 }
  3. { "_id" : 3, "value" : 4.32, "floorValue" : 4 }
  4. { "_id" : 4, "value" : -5.34, "floorValue" : -6 }