MongoDB query to skip n first documents?

To skip a specific number of documents in MongoDB, use the skip() method along with limit(). The skip() method bypasses the first n documents and returns the remaining documents from the result set.

Syntax

db.collection.find().skip(n).limit(count);

Sample Data

Let us create a collection with documents ?

db.demo246.insertMany([
    {"StudentFirstName": "Chris", "StudentLastName": "Brown"},
    {"StudentFirstName": "John", "StudentLastName": "Doe"},
    {"StudentFirstName": "John", "StudentLastName": "Smith"},
    {"StudentFirstName": "Carol", "StudentLastName": "Taylor"}
]);
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5e46b0d71627c0c63e7dba65"),
        ObjectId("5e46b0e21627c0c63e7dba66"),
        ObjectId("5e46b0ea1627c0c63e7dba67"),
        ObjectId("5e46b0f91627c0c63e7dba68")
    ]
}

Display all documents from the collection ?

db.demo246.find();
{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" }
{ "_id" : ObjectId("5e46b0e21627c0c63e7dba66"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5e46b0ea1627c0c63e7dba67"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
{ "_id" : ObjectId("5e46b0f91627c0c63e7dba68"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" }

Example: Skip First 2 Documents

Skip the first 2 documents and return only 1 document ?

db.demo246.find().skip(2).limit(1);
{ "_id" : ObjectId("5e46b0ea1627c0c63e7dba67"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }

Key Points

  • The skip(n) method bypasses the first n documents in the result set.
  • Combine with limit() to control the number of documents returned after skipping.
  • Useful for implementing pagination in applications.

Conclusion

Use skip() with limit() to bypass the first n documents and retrieve specific documents from a MongoDB collection. This combination is particularly useful for pagination and selective data retrieval.

Updated on: 2026-03-15T02:03:04+05:30

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements