MongoDB skip() method

In MongoDB, the skip() method is used to skip the first n document from the query result, where n is the number of records/documents to be skipped. If we want to skip a specific number of documents from the collection, the skip() method will skip the specified documents that we have mentioned in the MongoDB skip() method.


Syntax of skip() method

db.collection.find().skip(number)

The number is a positive integer that specifies maximum number of documents to skip.

Example of skip() method

Suppose we have the following 'students' collection.

{ "_id" : ObjectId("5b7ddc4f529cbc23546dc4c7"), "name" : "Jorz Rai", "age" : 10, "class" : "5A" }
{ "_id" : ObjectId("5b7ddf10529cbc23546dc4c8"), "name" : "Rana Soi", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4c9"), "name" : "Andy Joya", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4ca"), "name" : "Mary Soi", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4cb"), "name" : "Priska Somya", "age" : 12, "class" : "6A" }
mongodb skip

The given code will skip the first three matching documents and return the rest of the matches. If there are fewer than three documents in your collection, it will not return any documents.

db.students.find().skip(3);
Output of the above code:
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4ca"), "name" : "Mary Soi", "age" : 11, "class" : "5C" }
{ "_id" : ObjectId("5b7de0f4529cbc23546dc4cb"), "name" : "Priska Somya", "age" : 12, "class" : "6A" }
mongodb skip





Read more articles


General Knowledge



Learn Popular Language