$gte and $lte Operators in MongoDB: Ultimate Guide

MongoDB is used to store huge amounts of records in the form of documents having different fields.

Now suppose, a school using MongoDB for storing its student record has a collection of students, this collection contains hundreds of documents of students with different fields like Name, Class, Age, etc. Now for the vaccination school wants to select only students whose age is greater than 18, how to do that?

Among hundreds of documents, it is very hard to find the documents manually, even by using a regular MongoDB query. Here comes the $gte and $lte operators.

In this tutorial, we will learn about $gte and $lte operators, and their syntax and also see multiple examples to understand how to utilize them to find specific documents. We will also learn how to update documents using them.

MongoDB $gte and $lte Operators 

The $gte operator searches for values in a given field that are greater than or equal to the values supplied. On the other hand, the $lte operator finds documents whose field value is less than or equal to the provided field value. In short, both are opposite to each other.

It is important to note that only numerical values can be sent to the $gte and $lte operators.

The $gte and $lte operators may also be used in combination with find and update operation methods such as find(), update(), updateMany(), and others. Other operators, such as $set, can also be used with the $gte and $lte operators.

Let’s now see the syntax of both operators with the parameters they worked with.

Syntax of $gte Operator

As we already mentioned, it is used to filter documents where a given field is greater than or equal to the specified value. Following is the syntax of the $gte operator.

Syntax:

{field: {$gte: value}}

Here, the field is the field name where we are looking for the values that are greater than or equal to the specified value.

Syntax of $lte Operator

It is used to filter documents where a given field is smaller than or equal to the specified value. Following is the syntax of the $lte operator.

Syntax:

{field: {$lte: value}}

Here, the field is the field name where we are looking for the values that are smaller than or equal to the specified value.

Finding Documents Using the $gte and $lte Operators in MongoDB

In this section, we will use both operators one by one to find documents in a MongoDB collection.

Using $gte Operator

In this example, we will use the $gte operator to find documents whose numerical field values are greater than or equal to the specified values.

  • Start by running the MongoDB server.
  • In the Mongo shell, list all databases and move to the one you want to use.
show dbs
use droneShop
  • We have already created a MongoDB collection to demonstrate the example. Let us have a look at the documents residing in that collection before we proceed to the next step. We will be using the pretty() function which is used to format the query results with a proper indentation and produce more readable output.
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb2"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "There’s My Pheonix (Limited Edition) - 52",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}
  • Now, we will use the find() method with the $gte operator to look for documents whose price field value is greater than equal to the 60000. Below is the command for that:
> db.drones.find( { price: { $gte: 60000 } } ).pretty()
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb2"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "There’s My Pheonix (Limited Edition) - 52",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

We have successfully found all data where the “price” value is greater than or equal to 60000.

Using $lte Operator

Now, let’s use the find() method with the $lte operator to look for documents whose price field value is less than equal to 40000.

> db.drones.find( { price: { $lte: 40000 } } ).pretty()

{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}

Here we got the expected output.

Updating Documents Using the $gte and $lte Operators in MongoDB

Let’s use the $gte and $let operators to perform update operations in MongoDB.

Using the $gte Operator

In this example, we will use the $gte operator with updateMany() function to look for and update documents based on the condition that the value of the price field is greater than or equal to 75000. We will use the $set operator to update that value to 100000.

> db.drones.updateMany( { price: { $gte: 75000 } }, { $set: { price: 100000 } } )

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

The $gte operator found three documents and has updated them. Let’s have a look:

> db.drones.find({}).pretty()
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 100000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb2"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "There’s My Pheonix (Limited Edition) - 52",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

The $gte operator has successfully changed the value of price fields whose values are greater than or equal to 75000.

Using the $lte Operator

Let’s now use the $lte Operator with $set operator in updateMany() method to look for and update documents where the value of the price field is less than or equal to 70000. The updated value will be 50000.

> db.drones.updateMany( { price: { $lte: 70000 } }, { $set: { price: 50000 } } )

{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
  • Let us take a look at the documents now:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bae"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "FreezweX Dove 89",
        "price" : 50000,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076baf"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Triple6 TerraHawk 32",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb0"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sephra Ahnic Eagle - PY6",
        "price" : 100000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb1"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Pingoin Teal robin - Z42",
        "price" : 50000,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("616659d1ab1b93c0ca076bb2"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "There’s My Pheonix (Limited Edition) - 52",
        "price" : 100000,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

The $lte operator has now changed the price value of matched documents.

Read More: JavaScript Array – Understanding the Concept and Uses

Conclusion

In this article, we have discussed about the MongoDB comparison operators $gte and $lte with distinct examples. The examples also provide a comparison between the two. The gte operator is used when you want to return documents having a specified field value greater or equal to a given value whereas the lte operator in MongoDB is used when you want to return documents having a field value less than or equal to a given value. MongoDB supports two similar query operators – lt and gt, click here to read about it. At last, we hope you enjoyed reading the content.

References

Aneesha S
Aneesha S
Articles: 171