Docs HomeMongoDB Manual

$floor (aggregation)

On this page本页内容

Definition定义

$floor

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

$floor has the following syntax:具有以下语法:

{ $floor: <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, $floor returns null. 如果参数解析为null值或引用了缺失的字段,$floor将返回nullIf the argument resolves to NaN, $floor returns NaN.如果参数解析为NaN$floor将返回NaN

Example示例Results结果
{ $floor: 1 }1
{ $floor: 7.80 }7
{ $floor: -2.8 }-3

Example实例

Create a collection named samples with the following documents:使用以下文档创建一个名为samples的集合:

db.samples.insertMany(
[
{ _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 floor value:以下示例同时返回原始值和楼层值:

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

The operation returns the following results:该操作返回以下结果:

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