MongoDB

How to use limit query in MongoDB

MongoDB is a widely used NoSQL database that works on documents and collections to store, retrieve, or manipulate data. Hundreds of documents can be stored inside collections of MongoDB and big organizations do have such kinds of databases. Sometimes due to a large number of documents, the query execution takes time. In these situations, MongoDB offers to limit the number of documents using a limit() query. The limit query prints a limited number of documents and this limit is specified using a numeric value.

For instance, if you have assigned value “5” to limit query, then it will only show the first five documents of that specific collection. And this process resultantly saves time if you need only a few documents to refer to.

In this article, we have provided a deep insight into limit query in MongoDB and several examples are also given for effective understanding.

How to limit query works in MongoDB

The limit query works on the following syntax:

db.collection-name.find(<query>).limit(<numeric-value>)

The instance used in the above syntax is defined below:

  • collection-name: This refers to the name of the collection on which the “limit” method will be applied.
  • find(<query>): A well-known MongoDB method to print the result and <query> contains any condition to get the desired documents only.
  • limit(<numeric-value>): This is the core part of this syntax and it only takes numeric values that may vary from “-231” to “231“.

Furthermore, the examples in the upcoming section will provide a better overview of applying the limit() method to a collection.

Note: The collection name used in this tutorial is “employees“.

How to use the limit query in MongoDB

This section will provide several examples that show the application of limit query on a collection of documents. Firstly, let’s have a look at the documents in “employees” collection with the help of the following command:

> db.employees.find().pretty()

Example 1: Basic use

This example explains the basic use of limit query in MongoDB.

Command 1: The below-mentioned command will retrieve all the documents because no limit is specified in the limit() method.

> db.employees.find().limit()

Or if you use “limit(0)“; then it would also display all documents in a collection because the limit value is 0:

> db.employees.find().limit(0)

Command 2: contrary to the above command, a user can specify the limit number to retrieve only those documents. As in the following command, only the first three documents are retrieved by using the limit() method.

> db.employees.find().limit(3).pretty()

Note: The “pretty” method is used to get an arranged list of documents in a collection.

Example 2: Using limit query with skip

The limit query can be used with the skip method to get a more refined output. In MongoDB, the skip() method is exercised to get output after ignoring a few documents(a numeric number is used to tell how many documents will be ignored). For example, skip(2) will skip the first two documents. The command written below retrieves the documents that are at positions “3”, “4”, and “5”, because the first two documents have been ignored by the skip method.

> db.employees.find().skip(2).limit(3).pretty()

Example 3: Using limit query by specifying a condition

The following command will retrieve those documents that satisfy the following condition:

  • find({designation: {$eq: “Author”}}): This method will select only those documents that have “designation” field equals to “Author” value.
  • limit(2): After selection of documents in the above step; this limit query will only retrieve the first two documents from the above selected documents. Although there are three documents whose “designation” equals “Author” , only 2 are retrieved because of the limit(2) method.
> db.employees.find({designation: {$eq: "Author"}}).limit(2).pretty()

Alternative to limit query

MongoDB provides a wide range of operators and commands to process data. A $limit operator in the aggregation method can also do the same job as the limit() method does.

For instance, the command provided below will limit the number of retrieved documents to “3” using the $limit operator in the aggregation method.

The command is applied on the same collection “employees” and it is observed that the output is the same in both cases (using limit query or using $limit operator in aggregation).

>db.employees.aggregate({$limit: 3})

Conclusion

MongoDB offers detailed support for processing data using several commands and queries. The limit query in MongoDB has a vital role in making MongoDB a time-efficient database management system. In this guide of the MongoDB series, we have briefly illustrated the usage of limit query in MongoDB with a few examples. You can also extend the limit query functionality by using it with the skip method. The skip method ignores the specified number of documents whereas the limit will show those documents.

About the author

Adnan Shabbir