MongoDB Skip Documents

In this tutorial, we shall learn to skip a specified number of records that a query returns in the result. Skipping records is often useful when you have already shown the first N documents, and interested to show only the remaining documents. Skip command in conjunction with limit command is often used in pagination kind of stuff, where you shown only a limited number of results per page (or fetch).

MongoDB Skip Documents – To skip first N number of records that a query returns in the result, use cursor.skip() method. Skip method accepts a number as argument which indicates the starting number of records to be skipped in the query result.

Syntax of skip method

Following is the syntax of skip method :

When no value is provided, no documents are skipped, else the value provided in the argument would be considered.

Note : db.collection.find() returns cursor to the records. And skip() method can be applied on this cursor to skip the first N number of records as shown below :

 

Example : With a number as argument to skip method

Only two documents, as provided to the skip method, are skipped in the result.

 

Example : With nothing provided as argument to skip method

All documents are fetched in the result.

 

Example : skip() when used along with limit().

1 document is skipped and the result is limited to 2 documents.

 

Conclusion

In this MongoDB tutorialMongoDB Skip Documents, we have learnt to skip first N number of documents that a query returns in the result. In our next tutorial, we shall learn to sort documents.