MongoDB – skip() Method
In MongoDB, the skip() method will skip the first n document from the query result, you just need to pass the number of records/documents to be skipped. It basically removes the first n documents from the result set. For example, if your result has 5 records in it and you want to remove the first two records from it then you can use skip(2) at the end of the query. Or in other words, this method call on a cursor to control from where MongoDB starts returning the results.
Syntax :
cursor.skip(<offset>)
Or
db.collectionName.find(<query>).skip(<offset>)
Parameter:
This method can only take one parameter, i.e., offset. Here, offset is the number of the document to skip in the final result set.
Examples:
In the following examples, we are working with:
Database: w3wiki
Collections: gfg
Document: Eight documents contains the content
- Skip the first document
db.gfg.find().skip(1)
Here, we skip the first document by passing 1 in the skip method.
- Skip the two documents
db.gfg.find().skip(2)
Here, we skip first two documents by passing 2 in the skip method.
- Skip a document that matches the given filter
db.db.gfg.find({"content":/i/i}).skip(1)
Here, we skip the first document which contains ‘i’ as a character in it will be db.collectionName.find({“key”:/i/i}). Here, the first /i meant for the value which contains the character ‘i’ in it the second /i is for case insensitive like capital I or small i both will be included.
- Skip the first two documents that match the given filter
db.gfg.find({"content":/i/i}).skip(2)
Here, we skip the first two documents which contains ‘i’ as a character in it will be db.collectionName.find({“key”:/i/i}). Here, the first /i meant for the value which contains the character ‘i’ in it the second /i is for case insensitive like capital I or small i both will be included.
Contact Us