MongoDB $floor 运算符
MongoDB $floor 运算符 介绍
MongoDB 提供了多种算术表达式运算符。$floor 运算符就是其中之一。$floor 运算符用于返回小于或等于指定数字的最大整数。
MongoDB $floor 运算符 语法
{ $floor: <number> }
要点:
- 如果数字为 null,则 $floor 运算符返回 null。
- 如果输入的值引用了缺失的字段,则 $floor 运算符返回 null。
- 如果数字为 NaN,则 $floor 运算符返回 NaN。
MongoDB $floor 运算符 例子
在以下示例中,我们正在使用:
Database: Yiidian
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 运算符应用于“class VIII”学生的“fees”字段。
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 运算符(检索嵌入文档中的数据)
在这个例子中,我们将找到小于或等于“class 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
}
热门文章
优秀文章