MongoDB $ceil运算符
MongoDB提供了各种算术表达式运算符。$ceil
运算符是其中之一。$ceil
运算符用于返回大于或等于指定数字的最小整数。
语法
{ $ceil: }
重要要点:
- 如果数字为空,则$ceil运算符返回null。
- 如果输入的值指向一个缺失的字段,$ceil运算符返回null。
- 如果数字为NaN,则$ceil运算符返回NaN。
Example | Output |
---|---|
{ $ceil: 1 } | 1 |
{ $ceil: 9.1 } | 10 |
{ $ceil: -5.6 } | -5 |
示例
在以下示例中,我们正在使用:
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:使用$ceil运算符
在这个示例中,我们将找到女学生的成绩字段最接近或大于的最小整数。
db.student.aggregate(
[
{match: {gender : "Female"}},
{project:
{
std_name : 1,
class : 1,
grade : 1,
ceiling_grade: {ceil: "grade"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"),
"std_name" : "Tin",
"class" : "XI",
"grade" : 9.6,
"ceiling_grade" : "10"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds37412"),
"std_name" : "Olly",
"class" : "X",
"grade" : 8.4,
"ceiling_grade" : "9"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235489"),
"std_name" : "Oliva",
"class" : "VII",
"grade" : 9.7,
"ceiling_grade" : "10"
},
{
"_id" : ObjectId("56254d4fdf2222265r4g121235652"),
"std_name" : "Marry",
"class" : "VIII",
"grade" : 6.1,
"ceiling_grade" : "7"
},
示例2:缺少字段
在这个示例中,我们将$ceil操作符应用于“class XII”学生的“phone_no”字段。
db.student.aggregate(
[
{match: {class : "XII"}},
{project:
{
std_name : 1,
ceiling_phone: {ceil: "phone_no"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
"std_name" : "John",
"ceiling_phone" : null
},
示例3:空值
在这个示例中,我们将$ceil运算符应用于“class VIII”学生的“fees”字段。
db.student.aggregate(
[
{match: {class : "VIII"}},
{project:
{
std_name : 1,
ceiling_fees: {ceil: "fees"}
}
}
]
)
输出结果:
{
"_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
"std_name" : "Marry",
" ceiling_fees" : null
},
示例4:MongoDB $ceil运算符(检索嵌套文档中的数据)
在这个示例中,我们将找到“第九班”学生的grade.internal字段大于或等于的最小整数。
db.student.aggregate(
[
{match: {class : "IX"}},
{project:
{
std_name : 1,
class : 1,
grade.internal : 1,
ceiling_grade: {ceil: "grade.internal"}
}
}
]
)
输出:
{
"_id" : ObjectId("56254d4fdf2222265r4g121236456"),
"std_name" : "Paul",
"class" : "IX",
"grade" : {
"internal" : 4.5
}
"ceiling_grade" : 5
}