Docs 菜单

$floor (aggregation)

在此页面上

$floor

Returns the largest integer less than or equal to the specified number.

$floor 采用以下语法:

{ $floor: <number> }

<number> 表达式可以是任何有效的表达式,前提是它能解析为数字。有关表达式的更多信息,请参阅表达式运算符

如果参数解析为 null 的值或引用了缺失的字段,$floor 返回 null。如果参数解析为 NaN$floor 会返回 NaN

例子
结果

{ $floor: 1 }

1

{ $floor: 7.80 }

7

{ $floor: -2.8 }

-3

使用以下文档创建名为 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" } } }
])

操作返回以下结果:

{ "_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 }

在此页面上