MongoDB $floor 运算符
MongoDB中提供了不同类型的在聚合流水线阶段使用和$地板运算符就是其中之一rithmetic表达式运算符。此运算符用于查找小于或等于指定数字的最大整数。
句法:
{ $floor: }
在这里,数字是一个有效的表达式,直到它解析为一个数字。
- 如果输入的值为空,则此运算符将返回空。
- 如果输入的值为 NaN,则此运算符将返回 NaN。
- 如果输入的值为缺失字段,则此运算符将返回 null。
例子:
在以下示例中,我们正在使用:
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
使用 $floor运算符:
在这个例子中,我们会发现第l argest整数小于或等于perfoPoint场在发展部门的价值。
db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {perfoPoint: 1,
floorPoint: {$floor: "$perfoPoint"}}}])
在嵌入的文档中使用 $floor运算符:
在此示例中,我们将在 HR 部门中查找小于或等于 details.perfoPoint字段值的最大整数。
db.employee.aggregate([{$match: {department: "HR"}},
... {$project: {"details.perfoPoint": 1,
floorPoint: {$floor: "$details.perfoPoint"}}}])