How to Use the Mongoose Limit Function

Introduction

Usually, in a database, there is a huge number of data. Like in MongoDB, there can be thousands of documents. When we perform data retrieval operations, such as find, we may end up fetching these hundreds of documents in a single operation. It can cause problems. Sometimes we may need all the documents, and sometimes we may need only a few documents, say the first 50. For situations, mongoose provides the limit() function that can limit the number of documents in the output. In this article, we will discuss how to use the Mongoose limit function.

For performing HTTP endpoint testing, we will use the postman tool. You can download the postman tool from www.getpostman.com.

limit()

As the name suggests, the limit() function does some type of limitation. We will perform the find() operation with the limit() function on the following collection, details.

{ "_id" : ObjectId("5dbb2296e7971d398cd4d63d"), "name" : "John" }
{ "_id" : ObjectId("5dbb2299e7971d398cd4d63e"), "name" : "Sam" }
{ "_id" : ObjectId("5dbb229ee7971d398cd4d63f"), "name" : "Max" }
{ "_id" : ObjectId("5dbb22a2e7971d398cd4d640"), "name" : "John" }
{ "_id" : ObjectId("5dbb22a5e7971d398cd4d641"), "name" : "Sid" }
{ "_id" : ObjectId("5dbb22a8e7971d398cd4d642"), "name" : "Lisa" }
{ "_id" : ObjectId("5dbb22afe7971d398cd4d643"), "name" : "Sunny" }
{ "_id" : ObjectId("5dbb22b5e7971d398cd4d644"), "name" : "John" }
{ "_id" : ObjectId("5dbb22b9e7971d398cd4d645"), "name" : "Sam" }
{ "_id" : ObjectId("5dbb22bce7971d398cd4d646"), "name" : "John" }

There are ten documents in the details collection.

The following route handler will be invoked when the endpoint ‘/find’ will be executed.

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

As there is no query specified, the find() method will return all ten documents of the details collection. But what if we want only the first five documents? We can use the limit function and pass 5 to it. All we have to do is attach the limit() function at the end of the find() method.

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

Let’s execute this route using the postman tool.

Image from Gyazo

Yes! It returns only the first five documents.

The limit() function also works when a query is specified. In the details collection, there are four documents where the value of the name field is “John”. But we want only the first two in the output. Let’s use the limit() function.

router.route("/find").get(function(req, res) {
  details
    .find({ name: "John" }, function(err, result) {
      if (err) {
        res.send(err);
      } else {
        res.send(result);
      }
    })
    .limit(2);
});

This time, we provided a query – {“name” : “John”}, and passed 2 to the limit() function. Let’s execute this route.

Image from Gyazo

Yes! Only the first two documents are returned where the value of the name field is “John”.

Conclusion

So, we discussed the limit() function with the find() method. The limit() function is very simple and easy to use. No doubt, it is a very useful function when there are huge amounts of documents present in a collection.

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.