$gte and $lte Operators in MongoDB: Ultimate Guide

Gte And Lte Operators Featured Image

In this tutorial, I will cover the $gte and $lte operators in MongoDB.

While there are many operators in MongoDB, I will focus on the $gte and $lte operators today. Apart from being popular among beginners, it has also gained traction among seasoned developers.

The $gte operator searches for values in a given field that are greater than or equal to the values supplied. Only numerical values can be sent to the $gte and $lte operators.

They 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 be used with the $gte and $lte operators.

We can use the $lte operator to find documents whose field value is less than or equal to the provided field value.

This tutorial will teach the $gte and $lte operators with many examples so that you can understand how to utilize them. Let’s get this piece started.

The $gte and $lte Operators Syntaxes

Let us have a glance at the $gte and $lte operators’ syntaxes:

{field: {$gte: value}}
{field: {$lte: value}}

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

Let us start using the $gte and $lte operators in MongoDB with different examples.

Using $gte Operator

In this example, I 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
  • Let us have a look at all our documents residing in this collection before we proceed to the next step:
> 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, I will use the find() method with the $gte operator to look for documents whose field vales are greater than equal to the specified value:
> 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 drones whose prices are greater than equal to 60000.

Using $lte Operator

> 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"
                ]
        }
}

The $lte operator has resulted in a document whose value is less than or equal to the specified field value.

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

Let’s use the $gte and $let operators in MongoDB to update documents as per our requirements.

Using the $gte Operator

In this example, I will use the $gte operator with the update operation to look for and update documents that have a field value greater than or equal to the specified value.

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

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

The $gte operator found three documents and has updated it. 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 fields whose values are greater than equal to the specified values.

Using the $lte Operator

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

{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
  • Let us take a look:
> 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 document(s) that matched our query.

Read More: $gt and $lt Operators in MongoDB

Conclusion

In this article, we’ve learned how to use the $gte and $lte operators in MongoDB with distinct examples.