Javatpoint Logo
Javatpoint Logo

MongoDB $ceil Operator

MongoDB provides a variety of arithmetic expression operators. The $ceil operator is one of those operators. The $ceil operator is used to return the smallest integer greater than or equal to the specified number.

Syntax:

Important points:

  1. If the number is null, the $ceil operator returns null.
  2. If the entered value refers to a missing field, the $ceil operator returns null.
  3. If the number is NaN, the $ceil operator returns NaN.
Example Output
{ $ceil: 1 } 1
{ $ceil: 9.1 } 10
{ $ceil: -5.6 } -5

Examples:

In the following examples, we are working with:

{
        {
                "_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"
        },
}	

Example 1: Using $ceil operator

In this example, we are going to find the smallest integer greater than or equal to the grade field of the female student only.

Output:

{
                "_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"
 },

Example 2: Missing fields

In this example, we're applying the $ceil operator to the "phone_no" field of the "class XII" student.

Output:

{
                "_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
                "std_name" : "John",
                "ceiling_phone" : null
 },

Example 3: Null value

In this example, we're applying the $ceil operator to the "fees" field of the "class VIII" student.

Output:

{
                "_id" : ObjectId("56254d4fdf2222265r4g12ds3d636"),
                "std_name" : "Marry",
                " ceiling_fees" : null
 },

Example 4: MongoDB $ceil Operator (retrieving the data in the embedded document)

In this example, we are going to find the smallest integer greater than or equal to the grade.internal field of the "class IX" student.

Output:

{
             "_id" : ObjectId("56254d4fdf2222265r4g121236456"),
             "std_name" : "Paul",
             "class" : "IX",   
             "grade" : {
                          "internal" : 4.5
                             }
             "ceiling_grade" : 5
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA