$ceil (aggregation)

Definition

  • $ceil

New in version 3.2.

Returns the smallest integer greater than or equal to the specifiednumber.

$ceil has the following syntax:

  1. { $ceil: <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, $ceil returns null. If the argument resolves toNaN, $ceil returns NaN.

ExampleResults
{ $ceil: 1 }1
{ $ceil: 7.80 }8
{ $ceil: -2.8 }-2

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 ceilingvalue:

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

The operation returns the following results:

  1. { "_id" : 1, "value" : 9.25, "ceilingValue" : 10 }
  2. { "_id" : 2, "value" : 8.73, "ceilingValue" : 9 }
  3. { "_id" : 3, "value" : 4.32, "ceilingValue" : 5 }
  4. { "_id" : 4, "value" : -5.34, "ceilingValue" : -5 }