$ceil (aggregation)
Definition
New in version 3.2.
Returns the smallest integer greater than or equal to the specifiednumber.
$ceil
has the following syntax:
- { $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
.
Example | Results |
---|---|
{ $ceil: 1 } | 1 |
{ $ceil: 7.80 } | 8 |
{ $ceil: -2.8 } | -2 |
Example
A collection named samples
contains the following documents:
- { _id: 1, value: 9.25 }
- { _id: 2, value: 8.73 }
- { _id: 3, value: 4.32 }
- { _id: 4, value: -5.34 }
The following example returns both the original value and the ceilingvalue:
- db.samples.aggregate([
- { $project: { value: 1, ceilingValue: { $ceil: "$value" } } }
- ])
The operation returns the following results:
- { "_id" : 1, "value" : 9.25, "ceilingValue" : 10 }
- { "_id" : 2, "value" : 8.73, "ceilingValue" : 9 }
- { "_id" : 3, "value" : 4.32, "ceilingValue" : 5 }
- { "_id" : 4, "value" : -5.34, "ceilingValue" : -5 }