MongoDB $ceil 运算符

MongoDB $ceil 运算符 介绍 

MongoDB 提供了多种算术表达式运算符。$ceil 运算符就是这些运算符之一。$ceil 运算符用于返回大于或等于指定数字的最小整数。

MongoDB $ceil 运算符 语法

{ $ceil: <number> }  

要点:

  1. 如果数字为 null,则 $ceil 运算符返回 null。
  2. 如果输入的值引用了缺失的字段,则 $ceil 运算符返回 null。
  3. 如果数字为 NaN,则 $ceil 运算符返回 NaN。
例子 输出
{ $ceil: 1 } 1
{ $ceil: 9.1 } 10
{ $ceil: -5.6 } -5

MongoDB $ceil 运算符 例子

在以下示例中,我们正在使用:

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:使用 $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:Null值

在本例中,我们将 $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 运算符(检索嵌入文档中的数据)

在此示例中,我们将找到大于或等于“IX 类”学生的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
}

 

热门文章

优秀文章