MongoDB $floor 运算符
MongoDB 提供了各种算术表达式运算符,$floor
运算符就是其中之一。$floor
运算符用于返回小于或等于指定数字的最大整数。
语法
{ $floor: }
重要要点:
- 如果数字为null,则$floor运算符返回null。
- 如果输入值引用了一个缺失的字段,则$floor运算符返回null。
- 如果数字为NaN,则$floor运算符返回NaN。
示例
在以下示例中,我们使用的是:
Database: JavaTpoint
Collection: student
Document: Six documents that contain the details of the students
{
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"std_name" : "Tin",
"class" : "XI",
"gender" : "Female",
"Father_name" : "Jonny",
"fees" : 5000,
"exam_fees" : 500,
"age" : 17,
"grade" : 9.6,
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds37412"),
"std_name" : "Olly",
"class" : "X",
"gender" : "Female",
"Father_name" : "Mike",
"fees" : 6000,
"exam_fees" : 500,
"age" : 16,
"grade" : 8.4,
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
"std_name" : "John",
"class" : "XII",
"gender" : "Male",
"Father_name" : "Jin",
"fees" : 10000,
"exam_fees" : 500,
"age" : 18,
"grade" : 5.6
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235489"),
"std_name" : "Oliva",
"class" : "VII",
"gender" : "Female",
"Father_name" : "kinny",
"fees" : 3000,
"exam_fees" : 500,
"age" : 14,
"grade" : 9.7,
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235652"),
"std_name" : "Marry",
"class" : "VIII",
"gender" : "Female",
"Father_name" : "Jonny",
"fees" : null,
"exam_fees" : 500,
"age" : 15,
"grade" : 6.1,
"Result" : "Pass"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121236456"),
"std_name" : "Paul",
"class" : "IX",
"gender" : "Male",
"Father_name" : "Kane",
"fees" : 5000,
"exam_fees" : 500,
"age" : 16,
"grade" : {
"internal" : 4.5,
"external" : 7.4,
}
"Result" : "Pass"
},
}
示例1:使用$floor运算符
在这个示例中,我们将会计算女学生的成绩字段的小于或等于该字段的最大整数。
db.student.aggregate(
[
{match: {gender : "Female"}},
{project:
{
std_name : 1,
class : 1,
grade : 1,
floor_grade: {floor: "grade"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"std_name" : "Tin",
"class" : "XI",
"grade" : 9.6,
"floor_grade" : "9"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds37412"),
"std_name" : "Olly",
"class" : "X",
"grade" : 8.4,
"floor_grade" : "8"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235489"),
"std_name" : "Oliva",
"class" : "VII",
"grade" : 9.7,
"floor_grade" : "9"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235652"),
"std_name" : "Marry",
"class" : "VIII",
"grade" : 6.1,
"floor_grade" : "6"
},
示例2:缺少字段
在这个示例中,我们将$floor运算符应用于”class XII”学生的”phone_no”字段。
db.student.aggregate(
[
{match: {class : "XII"}},
{project:
{
std_name : 1,
floor_phone: {floor: "phone_no"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
"std_name" : "John",
"floor_phone" : null
},
示例3:Null值
在这个示例中,我们将$floor操作符应用于“VIII年级”学生的“费用”字段。
db.student.aggregate(
[
{match: {class : "VIII"}},
{project:
{
std_name : 1,
floor_fees: {floor: "fees"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
"std_name" : "Marry",
"floor_fees" : null
},
示例4:MongoDB $floor运算符(检索嵌入文档中的数据)
在这个示例中,我们要找到“IX班级”学生的grade.internal字段中小于或等于最大整数的值。
db.student.aggregate(
[
{match: {class : "IX"}},
{project:
{
std_name : 1,
class : 1,
grade.internal : 1,
floor_grade: {floor: "grade.internal"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g121236456"),
"std_name" : "Paul",
"class" : "IX",
"grade" : {
"internal" : 4.5
}
"floor_grade" : 4
}