MongoDB

MongoDB $gt $gte $lt $lte Operators

MongoDB supports a variety of query operators for searching and filtering documents in a collection. The “gt”, “gte”, “lt”, and “lte” operators are used to perform comparison queries on fields. The “gt” (greater than) operator returns documents where the specified field is greater than the specified value while the “gte” (greater than or equal to) operator returns documents where the specified field is greater than or equal to the specified value. On the other hand, the “lt” (less than) operator returns documents where the specified field is less than the specified value and the “lte” (less than or equal to) operator returns documents where the specified field is less than or equal to the specified value. There is little difference in all these operators but the method of using them in MongoDB is quite similar. Let us discuss that in this guide.

Let us say, you have all newly inserted records of the “Dummy” collection in your currently utilized database which is a total of 5 records with unique “_id”, “city”, and “count” fields.

test> db.Dummy.find({})

[

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 5 },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', count: 2 },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6d"), city: 'Italy', count: null  },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 8  },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi', count: 7  }

]

Example # 01: $gt Operator

The “gt” operator will be used to set or update the value of a particular field in a collection for a single record. For this, you have to execute the “updateOne” function of MongoDB with the “gt” operator set for a specific field. In our case, we have applied the update only to records with greater than the “3” value in their “count” field. The “set” operator is used to add a new “Desc” field with the value “Largest city in the world” for all those records having a “count” value greater than “3”. Only 1 record has been updated because we have tried the “updateOne” instruction.

test> db.Dummy.updateOne( { "count": { $gt: 3} }, { $set: { "Desc": "Largest city in the world"}} )

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 1,

  modifiedCount: 1,

  upsertedCount: 0

}

The first record of the “Dummy” collection has been updated only as per the execution result of the “find” function. The “Desc” field is added to the record with the city name “Texas”.

test> db.Dummy.find( { count: {$gt: 3} } )

[

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 5, Desc: 'Largest city in the world' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 8  },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi',  count: 7  }

]

Let us use the updateMany() function instead of updateOne() to update all the records of the “Dummy” collection with the “count” field values greater than “3”. The command below has been used for this purpose and we have have the acknowledgment.

test> db.Dummy.updateMany( { count: { $gt: 3} }, { $set : { "Desc": "Largest city in the world"}} )

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 3,

  modifiedCount: 2,

  upsertedCount: 0

}

Now, after running the “find” function instruction, once again with the “gt” operator, set on the “count” field where the value is greater than “3”, we have got the shown-below output. All the 3 records with a “count” value greater than “3” have now the “Desc” field as well.

test> db.Dummy.find( { count: {$gt: 3} } )

[

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 5, Desc: 'Largest city in the world' },
 
  { _id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 8, Desc: 'Largest city in the world' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi',  count: 7, Desc: 'Largest city in the world' },

]

Example # 02: $gte Operator

In this example, we will discuss the use of the “gte” operator of MongoDB: greater than or equal to. It will be used to search or update the records that are greater or equal to a particular value. The updateMany() function is used to update many documents in a MongoDB collection called “Dummy”, where the value of the “count” field is greater than or equal to 4. The count is greater than “4”. The “Set” operator will set the value of the “count” field to 2 for all of the matched documents. The update has been performed very smoothly.

test> db.Dummy.updateMany( { count: { $gte: 4} }, { $set : { "count": 2}} )

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 3,

  modifiedCount: 3,

  upsertedCount: 0

}

After displaying the updated records of the “Dummy” collection using the “find” function, we have have the result below. It shows that all the records now have the value “2” in the “count” field except the record “3” where the count is already null and does not meet the “gte” operator condition i.e. as the query above illustrated.

test> db.Dummy.find({})

[

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 2, Desc: 'Largest city in the world' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', count: 2 },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6d"),  city: 'Italy', count: null },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 2, Desc: 'Largest city in the world' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi',  count: 2,    Desc: 'Largest city in the world' }

]

Example # 03: $lt Operator

This time, we are going to utilize the “lt” operator of MongoDB to update records. The command beneath updates many documents in MongoDB using the updateMany() function once again, where the value of the “count” field is less than 4. The “Set” operator sets the value of the “count” field to 3 and the value of the “Desc” field to “Mid-sized city” for all of the matched documents. The acknowledgment shows that a total of 4 records is updated and the 1 that is left unchanged must have a “null” value at its “count” field.

test> db.Dummy.updateMany( { count: { $lt: 4} }, { $set: { "count": 3, "Desc": "Mid-sized city"}} )

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 4,

  modifiedCount: 4,

  upsertedCount: 0

}

After searching the records of the “Dummy” collection where the count field has all the values less than “4” via the “lt” operator, it returns the 4 records all having field “count” set to “3” and field “Desc” set to “Mid-sized city” as follows:

test> db.Dummy.find( { count: {$lt: 4} } )

[ { _id: ObjectId("63c8a1f94ff07039aa6cdf6b"), city: 'Texas', count: 3, Desc: 'Mid-sized city' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6c"), city: 'Los Angeles', count: 3, Desc: 'Mid-sized city' },

  { _id: ObjectId("63c8a1f94ff07039aa6cdf6e"), city: 'Istanbul', count: 3, Desc: 'Mid-sized city' }, { _id: ObjectId("63c8a1f94ff07039aa6cdf6f"), city: 'Delhi',  count: 3, Desc: 'Mid-sized city' } ]

Example # 04: $lte Operator

Starting from the query attached below, we have tried the “updateMany” function with its filter { count: { $lte: null} }. It matches all documents where the value of the “count” field is less than or equal to null. In our case, there is only one record with a “null” value in the field “count”.In the second argument, the “set” operator sets the value of the “count” field to 10 and the value of the “Desc” field to “Most Beautiful and Crowded” for all matched documents. The command below updates a single record in a “Dummy” collection, where the value of the “count” field is less than or equal to null.

test> db.Dummy.updateMany( { count: { $lte: null} }, { $set: { "count": 10, "Desc": "Most Beautiful and Crowded"}} )

{

  acknowledged: true,

  insertedId: null,

  matchedCount: 1,

  modifiedCount: 1,

  upsertedCount: 0

}

When you search for the record with a “count” field value greater than “8”, it returns a single record that has a “null” value previously.

test> db.Dummy.find( { count: {$gt: 8} } )

[ { _id: ObjectId("63c8a1f94ff07039aa6cdf6d"), city: 'Italy', count: 10, Desc: 'Most Beautiful and Crowded' } ]

Conclusion

This article covers the detail of the purpose of using the gt, gte, lt, and lte operators in MongoDB. The first example covers the “gt” operator and the second covers the “gte” operator with the help of MongoDB instructions. While the last two illustrations are covering the use of the “lt” and “lte” operators in detail with the help of the updateMany() function and the “set” operator.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.