MongoDB $floor Operator

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

Syntax:

Important points:

  1. If the number is null, the $floor operator returns null.
  2. If the entered value refers to a missing field, the $floor operator returns null.
  3. If the number is NaN, the $floor operator returns NaN.

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 $floor operator

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

Output:

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

Example 2: Missing fields

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

Output:

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

Example 3: Null value

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

Output:

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

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

In this example, we are going to find the largest integer less 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
                             }
             "floor_grade" : 4
}





Latest Courses