MongoDB Limit Records | MongoDB Skip() Method
Keeping you updated with latest technology trends, Join DataFlair on Telegram
We have learned how to insert and query a document in a collection in MongoDB. In this MongoDB Tutorial, we are going to see what is MongoDB limit and how to limit record in MongoDB. Moreover, we will discuss MongoDB Skip() Method with an example. At last, we discuss some MongoDB Limit examples.
So, let’s start with MongoDB Limit Records.
2. MongoDB Limit Records
When we query a collection, it shows all the documents contained in it. The limit cursor is used for retrieving only numbers of documents that we need. We can use MongoDB limit() method to specify the number of documents we want it to return.
When we query a document using the db.collection_name.find() method, we can append this method to specify the limit.
Let’s understand this with an example.
First, run a query without specifying the limit.
We see that it shows all the documents it has. There are total 6 documents in the result. Now, let’s run a query for the same collection after specifying the limit in MongoDB.
We see that it shows only 4 documents as the limit specified.
3. MongoDB Skip() Method
When we use the MongoDB limit() method, it shows from the beginning of the collection to the specified limit. If we want it to start not from the beginning and skipping some documents, we can use the Skip() method for this task.
To do this, we need to add the skip() in the cursor and specify the number of documents that we want to skip.
Let’s query the same collection after adding MongoDb skip().
We see that the first document has been skipped from the collection.
We can use MongoDB skip() method without the limit specified. It can be used with any query run.
Let’s run a query for the same collection we have discussed above.
This shows that MongoDB skip can be used with any query without using MongoDB limit.
So, this was all about MongoDB Limit Tutorial. Hope you like our explanation.
Hence, we saw that how limiting helps us to see the documents easy. MongoDB Limit is a very useful function. We can use MongoDB limit when the number of documents is so many. This will help us to search the specific documents easily. In conclusion, we discuss MongoDB Skip Method. In the fore coming articles, we will discuss indexing and advanced indexing and it’s limitation. Furthermore, if you have any query, feel free to ask in a comment section.
Related Topic – MongoDB Indexing