Updated February 27, 2023
Introduction to MongoDB Skip()
MongoDB skip() is used when we required a certain number of results after a certain number of documents simultaneously we have use skip method in MongoDB. If we want to skip a certain number of documents from the collection, skip method will skip the specified documents that we have used with the MongoDB skip method. Skip method in MongoDB will same as an offset used in SQL language, we can also use skip method with the limit. After using the skip method with a limit in MongoDB, it will skip the specified number of documents, and then it will return the result. In this topic, we are going to learn about MongoDB Skip().
Syntax
Below is the syntax of the MongoDB skip method.
1. Skip method
db.collection_name.find ().skip(number of documents that we have skipping)
2. Skip method with a limit
db.collection_name.find ().skip(number of documents that we have skipping).limit(number of documents that we have fetching)
3. Skip method with limit and pretty method
db.collection_name.find ().skip().limit().pretty();
Below is the parameter description syntax of the MongoDB skip method.
1. Collection name: Collection name is defined as that we have used to fetch the data along with the skip method. We can use any collection name to fetch the data from the collection. We have also use limit and pretty method with skip to fetch the data from the collection.
2. Find: Find method is used with the collection name to fetch the documents using the skip method. If we have not used any skip and limit with the find method, it will return all the collection documents.
3. Limit: The limit method is used to fetch the specific number of the document from the collection. We can use the limit method with the skip method to skip the specific number of documents and return the document of the limited number after skipping the documents.
4. Pretty: Pretty method is used with the skip method to retrieve the result of the collection in formatted order. The pretty method is handy and important.
5. Skip: Skip method in MongoDB used to retrieve document after skipping the specified documents from the collection. The skip method in MongoDB will skip the specified number of documents.
How does MongoDB Skip() method work?
- Below is the working of the skip method in MongoDB.
- The limit method will be used to return the maximum number of results from the collection, while the skip method is used to skip the number of documents from the collection in MongoDB.
- If we have one collection name as a student, student collection contains a hundred documents in it.
- In student collection, we have to fetch data as 10 documents; simultaneously, we have used collection limits. But the limit will fetch the data as starting 10 documents.
- When we have fetching data from the series as 11 to 50 then the limit is not working simultaneously, then at that time we have use limit with the number as 40, and skip is used with the number as 10.
- After using skip with 10, it will skip the first 10 documents from the collection and retrieved result of documents after 10.
- If we have not used limit with skip then only the first 10 documents are skipped, after skipping 10 documents, all 90 documents result will be retrieved in result set.
- Skip method with find method is not display the result in a formatted way, to display the result with formatted way then we have used a pretty method with a skip in MongoDB.
- Skip in MongoDB will skip the specified number of documents that were pass documents into the first stage, and remaining documents will pass into the next stage in the pipeline order.
- Skip in MongoDB will have the following form are as follows.
{ $skip: <+ve number>}
- The negative number is not allowed with a skip in MongoDB; it will display the message as “Skip value must be non-negative but received: -1”.
- In the below example, we have used value as -1 with skip method; it will not display the output result because the negative value is not allowed in the skip method.
- We need to define only a positive number with the skip method when retrieving data from the collection.
db.test_skip.find().skip(-1)
Examples of MongoDB Skip()
Below is the example of the skip method in MongoDB.
Example #1 – Skip method
db.test_skip. find(). skip (5)
In the above example, we have skipped the first five documents from the test_skip collection. After skipping 5 documents remaining all the documents will display in output as a result.
We have not used a pretty method to display the result, so the output is not received in formatted order.
Example #2 – Skip method with a limit
db.test_skip. find(). skip (5). limit (5)
In the above example, we have skipped the first five documents from the test_skip collection, and also we have used limit as 5 with the skip method. After using limit with a skip, it will display documents from the id 6 to id 10.
Example #3 – Skip method with limit and pretty
In the below example, we have to retrieve documents using the limit method with the skip and pretty method in MongoDB.
After using the pretty method, documents will be displayed in formatted order.
db.test_skip. find(). skip (8). limit (2). pretty ()
Recommended Articles
This is a guide to MongoDB Skip(). Here we discuss how MongoDB Skip() method works with appropriate syntax with respective examples for better understanding. You may also look at the following articles to learn more –