How to use the mongoose $gte operator

Introduction

A query is an important part of CRUD operations. In most cases, we have to provide a query to get the desired results. A query is specified inside curly brackets. The complexity of a query depends upon the requirements. It can be simple and it can be complicated as well. For example, if we want all the documents of a collection where the value of a certain field is equal to a certain value, we can provide the following query in the find() method.

{"age" : 25}

According to this query, the result will contain all the documents where the value of the age field is 25. But what if we want all the documents where the value of the age field is greater than 25? or greater than 25? or where it lies within a certain range? Many such situations can arise. Mongoose provides various operators to encounter such situations. One of such operators is $lge operator. It stands for “greater than equal to” operator. The $gte operator is one of the most commonly used operators in a query. This operator returns the documents where the value of the specified field is greater than or equal to the given value. In this article, we will discuss how to use the mongoose $gte operator.

We will use the postman tool for testing. You can download it from www.getpostman.com.

$gte operator

Observe the following collection.

{ "_id" : ObjectId("5df91907256fc2afde3b6d90"), "name" : "John", "age" : 21, "location" : "New York" }
{ "_id" : ObjectId("5df9190b256fc2afde3b6d91"), "name" : "Sam", "age" : 25, "location" : "Chicago" }
{ "_id" : ObjectId("5df91911256fc2afde3b6d92"), "name" : "Lisa", "age" : 27, "location" : "Texas" }
{ "_id" : ObjectId("5df91915256fc2afde3b6d93"), "name" : "Max", "age" : 23, "location" : "New York" }
{ "_id" : ObjectId("5df9191b256fc2afde3b6d94"), "name" : "Ronn", "age" : 29, "location" : "Texas" }

The above collection contains five documents. In three documents, the value of the age fields is greater or equal to 25. We will fetch these documents using the find() method with the $gte operator.

We need documents where the value of the age field is greater than or equal to 25. In the query, we will use the $gte operator. The following route handler will be invoked when the endpoint ‘/fetchdata’ is executed.

router.route("/fetchdata").get(function(req, res) {
  employees.find({}, function(err, result) {
    if (err) {
      res.send(err);
    } else {
      res.send(result);
    }
  });
});

For the time being, there is no condition in the find() method. It will fetch all the documents in the collection. So let’s add the $gte operator to get the desired results.

To use the $gte operator, we need to provide it as a value to the field according to which we want to filter the result. In our case, that field is the age field. The $gte operator is specified as an object and an integer value is given to it. The result will be filtered according to this value. Let’s see how the query will look in our case.

{"age"  : {$gte : 25}

Let’s add it in the find() method and see the result.

Image from Gyazo

Yes! It returns all the documents where the value of the age field is greater than or equal to 25.

$gt operator

The $gt operator stands for “greater than” operator. It works in the same way as the $gte does, but with a single exception. It only returns the documents where the value of the specified field is greater than the given value. Let’s replace the $gte operator with the $gt operator and check what happens.

router.route('/fetchdata').get(function(req,res){

    employees.find( {{"age"  : {$gt : 25}}, function(err, result){

        if(err){
            res.send(err)
        }
        else{


            res.send(result)
        }

        });

})

Image from Gyazo

This time it returned only those documents where the value of the age field is greater than 25 and did not return the document where the value is equal to 25.

Very similar functionality will be provided for when you are looking for the corresponding less than $lt or less than or equal to $lte.

Conclusion

Thank you for reading this brief article on how to use the greater than or equal to operator in mongoose ($gte). We showed you how to use it as well as an example of the greater than operator $gt and how it would be translated to less than and less than or equal to ($lt, $lte).

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.