How to use the mongoose $lte operator

Introduction

In this article we’ll be discussing the mongoose $lte operator, what it does, and how to use it with a simple demonstration.

Mongoose provides several methods that can be used while performing CRUD operations. In most cases, we have to provide a query to get the desired results. A query is specified inside curly brackets. A query 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 do it as follows.

{"age" : 30}

According to this query, the result will contain all the documents where the value of the age field is 30. But what if we want all the documents where the value of the age field is less than 30? or greater than 30? 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 $lte operator. It stands for “less than equal to” operator. The $lte 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 less than or equal to the given value. In this article, we will discuss how to use the $lte operator in mongoose.

$lte operator (Less than or equal to)

Observe the following collection.

{ "_id" : ObjectId("5df751cf8f0f97d8dbe399f9"), "name" : "John", "age" : 21 }
{ "_id" : ObjectId("5df751d88f0f97d8dbe399fa"), "name" : "Lisa", "age" : 24 }
{ "_id" : ObjectId("5df751f38f0f97d8dbe399fd"), "name" : "Ronn", "age" : 30 }
{ "_id" : ObjectId("5df751fe8f0f97d8dbe399fe"), "name" : "Sam", "age" : 46 }
{ "_id" : ObjectId("5df7521e8f0f97d8dbe399ff"), "name" : "Max", "age" : 32 }

There are five documents and in three of them, the value of the age field is less than or equal to 30. We will use the find() method to fetch these documents.

We need documents where the value of the age field is less than or equal to 30. In the query, we will use the $lte operator. The following route handler will be invoked when the endpoint ‘/fetchdata’ is executed. We will use the postman tool for testing. You can download it from www.getpostman.com.

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

Currently, the find() method has an empty object as the query. Let’s add the $lte operator.

To use the $lte 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 $lte 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"  : {$lte : 30}

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

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

$lt operator (less than)

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

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

   employees.find( {{"age"  : {$lt : 30}}, function(err, result){

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


           res.send(result)
       }

       });

})

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

Conclusion

In this article we discussed the mongoose $lte operator which is a useful less than or equal to filter. We also showed the less than operator $lt which is very similar.

We hope you were able to use this simple demo and apply it to your situation. Thank you for joining us at the Object Rocket knowledge base.

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.