Javatpoint Logo
Javatpoint Logo

MongoDB $lte Operator

What is the $lte operator in MongoDB?

MongoDB provides a variety of comparison query operators. The $lte (less than equal to) operator is one of those operators. The $lte operator is used to select documents where the value of the field is less than or equal to (i.e. <=) the given value. You can use this operator in the update, find, like method as per your requirements.

Syntax of the $lte operator:

Examples:

In the following examples, we are working with:

>db.employee.find()
        {
                "_id" : 1,
                "name" : "John",
                "father_name" : "Thomas",
                "department" : "Contain Writting",
                "experience" : 2,
                "mobile_no" : 9856321478,
                "gender" : "Male",
                "salary" : 22000,
                "age" : 26
        }
        {
                "_id" : 2,
                "name" : "William",
                "father_name" : "Rebort",
                "department" : "Softwre Tester",
                "experience" : 1,
                "mobile_no" : 7896541478,
                "gender" : "Male",
                "salary" : 20000,
                "age" : 21
        }
        {
                "_id" : 3,
                "name" : "Ava",
                "father_name" : "William",
                "department" : "Marketing manager",
                "experience" : 5,
                "mobile_no" : 8789654178,
                "gender" : "Female",
                "salary" : 36500,
                "age" : 25 
        }
        {
                "_id" : 4,
                "name" : "Olivia",
                "father_name" : "Noah",
                "department" : null,
                "experience" : 4,
                "mobile_no" : 9045641478,
                "gender" : "Female",
                "salary" : 30000,
                "age" : 27 
        }
        {
                "_id" : 5,
                "name" : "Elijah",
                "father_name" : "John",
                "department" : "HR",
                "experience" : 0,
                "mobile_no" : 6589741230,
                "gender" : "Male",
                "salary" : 15000,
                "age" : 20
        }
        {
                "_id" : 6,
                "name" : "John",
                "father_name" : "Liam",
                "department" : "Softwre Tester",
                "experience" : 10,
                "mobile_no" : 9014536987,
                "gender" : "Male",
                "salary" : 55000,
                "age" : 30 
        }

Example 1: Using $lte operator

In this example, we are looking for documents whose salary scale is equal to or less than 30000.

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_id" : 2,
    "name" : "William",
    "father_name" : "Rebort",
    "department" : "Softwre Tester",
    "experience" : 1,
    "mobile_no" : 7896541478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 21
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 30000,
    "age" : 27 
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 15000,
    "age" : 20
}

Example 2: Another field

In this example, we're looking for documents whose age is less than or equal to 26.

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_id" : 2,
    "name" : "William",
    "father_name" : "Rebort",
    "department" : "Softwre Tester",
    "experience" : 1,
    "mobile_no" : 7896541478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 21
}
{
    "_id" : 3,
    "name" : "Ava",
    "father_name" : "William",
    "department" : "Marketing manager",
    "experience" : 5,
    "mobile_no" : 8789654178,
    "gender" : "Female",
    "salary" : 36500,
    "age" : 25 
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 15000,
    "age" : 20
}

Example 3: Update the value using the $lte operator

In this example, we're updating the salary field for employees who have less than or equal to 3 years of work experience.

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 26
}
{
    "_id" : 2,
    "name" : "William",
    "father_name" : "Rebort",
    "department" : "Softwre Tester",
    "experience" : 1,
    "mobile_no" : 7896541478,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 21
}
{
    "_id" : 5,
    "name" : "Elijah",
    "father_name" : "John",
    "department" : "HR",
    "experience" : 0,
    "mobile_no" : 6589741230,
    "gender" : "Male",
    "salary" : 20000,
    "age" : 20
}






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