$ceil (aggregation)

On this page本页内容

Definition定义

$ceil

New in version 3.2.版本3.2中的新功能。

Returns the smallest integer greater than or equal to the specified number.返回大于或等于指定数字的最小整数。

$ceil has the following syntax:$ceil语法如下所示:

{ $ceil: <number> }

The <number> expression can be any valid expression as long as it resolves to a number. <number>表达式可以是任何有效的表达式,只要它解析为一个数字。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

If the argument resolves to a value of null or refers to a field that is missing, $ceil returns null. 如果参数解析为null值或引用缺少的字段,$ceil返回nullIf the argument resolves to NaN, $ceil returns NaN.如果参数解析为NaN,则$ceil返回NaN

Example示例Results结果
{ $ceil: 1 } 1
{ $ceil: 7.80 } 8
{ $ceil: -2.8 } -2

Example示例

A collection named samples contains the following documents:名为samples的集合包含以下文档:

{ _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 ceiling value:以下示例返回原始值和上限值:

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 }