$gt and $lt Operators in MongoDB: Basic Guide

People are usually confused about which to choose MongoDB or MySQL. Well, here is the moment of truth. MongoDB provides us with so many useful operators that you can perform any type of action using them. Whether it is extremely simple or exceptionally complex, you can easily find an operator for it. We have covered a lot of its operators and yet, the list is never-ending and that is the power of Mongo.

In this article, we will cover another two useful comparison operators, $gt and $lt, somewhat similar to the operator we have already covered in the $gte and $lte Operators in MongoDB article.

We’ll start this article with an introduction to comparison operators, then we’ll look at the $gt and $lt syntax and then demonstrate them with the help of several examples. let’s get started.

Comparison Query Operators

MongoDB has various comparison operators to match documents on the basis of some comparisons with specified values or expressions.

The following is the comparison operators table in MongoDB:

OperatorsDescription
$eqQuery documents that field values are equal to a specified value.
$gtQuery documents that field values are greater than a specified value.
$ltQuery documents that field values are less than a specified value.
$gteQuery documents that field values are greater than or equal to a specified value.
$lteQuery documents that field values are less than or equal to a specified value.
$inQuery documents that field values match any of the values in a specified array.
$ninQuery documents that field values do not match any of the values in a specified array.
$neQuery documents that field values are not equal to a specified value.

In the above table you have seen a short introduction to $gt and $lt, let’s now deeply understand it.

MongoDB $gt and $lt Operators 

While the list of operators in MongoDB is extensive, the $gt and $lt operators proved to be significantly valuable. Aside from being popular among novices, it has also garnered momentum among experienced developers. 

The $gt operator allows us to find the documents where a specific field value is greater than an argument value. This operator can only accept numerical values. It can also be used with find and update operation functions like find(), update(), updateMany(), etc

On the other hand, the $lt operator allows us to match documents whose specified field value is less than the argument value. The rest of the things are the same.

Note: The $gt and $lt operators can be clubbed with other operators. In the upcoming examples, we have used it with the $set operator to update values.

Syntax of $gt Operator

The $gt filters documents having a specific field value greater than the value passed as an argument. Following is the syntax of the $gt operator.

{field: {$gt: value}}

Here, the field is the field name where we are looking for the values that are greater than the argument value.

Syntax of $lt Operator

The $lt filters documents having a specific field value less than the value passed as an argument. Following is the syntax of the $lt operator.

{field: {$lt: value}}

Here, the field is the field name where we are looking for the values that are less than the argument value.

Finding Documents Using the MongoDB $gt and $lt Operators

Let us now look at how to find documents using the $gt and $lt operators in the MongoDB shell.

Setup:

1. Start the MongoDB shell.

2. Execute the following commands to list all the available databases & move into one of them.

show dbs
use droneStore

We have already created a collection “drones” possess numerous documents for demonstration.

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.

Following are the documents inside “drones”: 

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

Using the $gt Operator

Let’s now use the $gt operator to find documents whose “price” field is greater than 80000. For that, we will club the $gt operator with find() method.

> 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 the $lt Operator

Now, let’s use the $lt operator to find documents whose “price” field is less than 30000. 

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

We found only one document because only this document’s price value is less than 30000.

Updating Documents Using the MongoDB $gt and $lt Operators

Let’s now use the $gt and $lt operators to update documents utilising the $set operator. We will be using the same collection “drones” which used in the previous section.

Using the $gt Operator

In this example, we will be using the $gt operator with update() function to look for and update a document based on the condition that the value of the price field is greater than 89000. We will also use the $set operator to update that value to 90000.

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

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

Let’s take a look at the documents now:

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

Note that only those documents where the value of the price field was greater than 89000 are updated.

Using the $lt Operator

Let’s now use the $lt operator with update() function to look for and update a document based on the condition that the value of the price field is less than 30000 and then use the $set operator to update that value to 20000.

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

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

Let’s take a look at the documents now:

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

In the above code, we can see that the price data of matched documents has now changed to 20000.

Conclusion

In this tutorial, we’ve learned about two MongoDB operators: gt and lt. gt is used when you want to find documents where a specific field value is greater than the argument value and the lt operator offers just the opposite, it selects on the basis of less than. We have seen both of these methods with distinct examples. Hope those examples helped you understand them.

Read More: Top 5 Python Libraries for Machine Learning and Data Science

References

Aneesha S
Aneesha S
Articles: 173