$gt and $lt Operators in MongoDB: Basic Guide

Gt And Lt Operators Featured Image

In this tutorial, I will cover the $gt and $lt operators in MongoDB.

While the list of operators in MongoDB is extensive, I will concentrate on the $gt and $lt operators today. Aside from being popular among novices, it has also garnered momentum among experienced developers.

The $gt operator allows us to look for values of a specified field that are greater than the specified values. This operator can only accept numerical values. It can also be used with find and update operation functions like find(), update(), updateMany() and the rest. The $gt operator can be clubbed with other operators such as $set.

The $lt operator, on the other hand, allows us to match documents whose specified field value is less than the field value.

This guide shall explain the $gt and $lt operators with different examples so you will understand how to use them. Let us get started.

Also read: $gte and $lte Operators in MongoDB: Ultimate Guide

The $gt and $lt Operators Syntaxes

Let us have a glance at the $gt and $lt operators’ syntaxes:

{field: {$gt: value}}
{field: {$lt: value}}

Finding Documents Using the $gt and $lt Operators in MongoDB

Let us start using the $gt and $lt operators in MongoDB with different examples.

Using $gt Operator

In this example, I will use the $gt operator to find documents whose numerical field values are greater than 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 droneStore
  • 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("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161e33213ca7f1ba14cfe22"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Adam Levi Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}
  • Now, I will use the find() method with the $gt operator:
> db.drones.find( { price: { $gt: 80000 } } ).pretty()

{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 89500,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161e33213ca7f1ba14cfe22"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Adam Levi Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

We have successfully found all drones whose prices are above 80000.

Using $lt Operator

> db.drones.find( { price: { $lt: 30000 } } ).pretty()

{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}

The $lt operator has resulted in a document whose value is less than the specified field value.

Updating Documents Using the $gt and $lt Operators in MongoDB

Let us now use the $gt and $lt operators in MongoDB to update documents using the $set operator.

Using the $gt Operator

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

> db.drones.update( { price: { $gt: 89000 } }, { $set: { price: 90000 } } )

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

The $gt operator found one document and has updated it. Lets have a look:

> db.drones.find({}).pretty()
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 90000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 29500,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161e33213ca7f1ba14cfe22"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Adam Levi Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

The $gt operator has successfully changed the value of a field that is greater than the specified.

Using the $lt Operator

> db.drones.update( { price: { $lt: 30000 } }, { $set: { price: 20000 } } )

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  • Let us now take a look:
> db.drones.find({}).pretty()
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1e"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Shadow X-copter C32",
        "price" : 69500,
        "weight" : "65 kilograms",
        "additionalDetails" : {
                "material" : "glass fiber",
                "moreUses" : [
                        "Delivery",
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe1f"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tranzm8 Dree RFD",
        "price" : 78500,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "carbon fiber",
                "moreUses" : [
                        "Land Inspection",
                        "Water Inspection"
                ]
        }
}
{
        "_id" : ObjectId("6161c46c13ca7f1ba14cfe20"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Sierra Gane’s Qatacopter J90",
        "price" : 90000,
        "weight" : "10 kilograms",
        "additionalDetails" : {
                "material" : "aluminum",
                "moreUses" : [
                        "Monitoring or Inspection",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161dcb013ca7f1ba14cfe21"),
        "utility" : [
                "Photography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Jennifer Steele’s Pegacopter PU-6",
        "price" : 20000,
        "weight" : "14 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography",
                        "Recreation"
                ]
        }
}
{
        "_id" : ObjectId("6161e33213ca7f1ba14cfe22"),
        "utility" : [
                "Security",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Tony Blaire Hawk Grim 4590",
        "price" : 87600,
        "weight" : "13 kilograms",
        "additionalDetails" : {
                "material" : "polysterene",
                "moreUses" : [
                        "Land Inspection",
                        "Photography"
                ]
        }
}

The $lt operator has now changed the price value of document(s) whose value was lesser than 30000.

Conclusion

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