Javatpoint Logo
Javatpoint Logo

MongoDB $gt Operator (greater than)

What is the $gt operator in MongoDB?

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

Syntax of the $gt operator:

Examples:

In the following examples, we are working with:

Output:

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

In this example, we are looking for those documents whose salary scale is greater than 20000.

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_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" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 55000,
    "age" : 30 
}

Example 2: Another field

In this example, we are looking for those documents whose age is greater than 25.

Output:

{
    "_id" : 1,
    "name" : "John",
    "father_name" : "Thomas",
    "department" : "Contain Writting",
    "experience" : 2,
    "mobile_no" : 9856321478,
    "gender" : "Male",
    "salary" : 22000,
    "age" : 26
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 30000,
    "age" : 27 
}
{
    "_id" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 55000,
    "age" : 30 
}

Example 3: Update the value using the $gt operator

In this example, we are updating the salary field of those employees who have greater than 3 years of experience.

Output:

{
    "_id" : 3,
    "name" : "Ava",
    "father_name" : "William",
    "department" : "Marketing manager",
    "experience" : 5,
    "mobile_no" : 8789654178,
    "gender" : "Female",
    "salary" : 50000,
    "age" : 25 
}
{
    "_id" : 4,
    "name" : "Olivia",
    "father_name" : "Noah",
    "department" : null,
    "experience" : 4,
    "mobile_no" : 9045641478,
    "gender" : "Female",
    "salary" : 50000,
    "age" : 27 
}
{
    "_id" : 6,
    "name" : "John",
    "father_name" : "Liam",
    "department" : "Softwre Tester",
    "experience" : 10,
    "mobile_no" : 9014536987,
    "gender" : "Male",
    "salary" : 50000,
    "age" : 30 
}






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