limit query in mongodb

How to Write Limit Query in MongoDB

MongoDB is a popular NoSQL database used by organizations and websites all over the world. It does not use SQL syntax for database querying. It stored all its data as documents grouped into collections. But often you may need to limit the results of your MongoDB queries. This is especially true if you have a lot of documents, or you just want to test a query before putting it into production. In this article, we will learn how to write Limit query in MongoDB.


How to Write Limit Query in MongoDB

Here is the syntax of query to limit query results in MongoDB.

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

In the above command, collection-name is the name of the collection upon which you want to apply the limit. <query> is the set of columns that you want to fetch in your query and <numeric-value> is the number of documents that you want to be returned as query result. The numeric value can vary from -231 to 231

Now let us look at some use cases of limit query. For our examples, we will use sales collection.


1. List All Documents

Let us say you want to view all documents in sales collection. In this case, use limit(0) as shown in the following query. We use find() function to list all columns of the documents.

db.sales.find().limit(0)

Alternatively, you can also use limit() as shown below.

db.sales.find().limit()


2. Limit Documents

If you want to list only the first 5 documents of your result, use limit(5) as shown below.

db.sales.find().limit(5)

You can also add more functions after limit(). Here is an example to pretty print the output of query.

db.sales.find().limit(5).pretty()

If you want to select only product_name =’abc’ from sales collections, you can add the query {product_name:’abc’} in find() function.

db.sales.find({product_name:'abc'}).limit(5)

In the above query, MongoDB will fetch documents where product_name is ‘abc’ and limit the result to top 5 documents.


3. Using Limit with Skip

What is you want to limit query results but skip the first few documents? In such cases, you can use skip() function to skip rows. Here is a query to limit query documents to 3 documents after skipping the first 2 documents.

db.sales.find().skip(2).limit(3)

Please note, you have to add skip() function to the left of limit() function, because the order of execution is from left to right.


4. Alternative to Limit

There is also an alternative to limit() function in MongoDB. If you are using aggregate function, then you can use $limit parameter, instead of using limit() function. Here is an example to get the same result as limit() function above, using aggregate function.

db.sales.aggregate({$limit:5})

The above query gives same result as

db.sales.find().limit(5)

In this article, we have learnt the different ways to write limit query in MongoDB. MongoDB is a powerful database for Non-structured data and limit query plays an important role in making its queries fast and efficient.

Also read:

How to Copy/Transfer Data Between Databases in MySQL
How to Setup Uwsgi with NGINX for Python
How to Check MySQL Version in Ubuntu
How to Clear Temp Files in Ubuntu
Git Pull vs Fetch

Leave a Reply

Your email address will not be published. Required fields are marked *