MongoDB Skip Documents

MongoDB Skip Documents – To skip the first N number of records that a query returns in the result, use the cursor.skip() method. The skip method accepts a number as an argument and moves the cursor forward by that many documents before returning the remaining result.

In this tutorial, we shall learn to skip a specified number of records that a query returns in the result. We shall also see how skip() works with limit(), why sort() is important for predictable pagination, and how $skip is used in aggregation pipelines.

Skipping records is often useful when you have already shown the first N documents and want to show the next set of documents. For example, if a page displays 10 results, the second page can skip the first 10 records and then return the next 10 records.

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() method syntax for find() cursor

Following is the syntax of skip method :

cursor.skip(N)

Here, N is the number of documents to skip from the beginning of the cursor result. If N is 2, MongoDB skips the first two documents from the cursor and starts returning documents from the third matching document.

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.

db.collection.find().skip(N)

You can also use skip() after a filter condition. In the following syntax, MongoDB first matches documents using the filter and then skips the specified number of matching documents from the cursor.

</>
Copy
db.collection.find({ field: value }).skip(N)

Example 1 – Skip first two documents in MongoDB

In the following example, db.people.find().skip(2) skips the first two documents returned by the cursor and returns the remaining documents.

> db.people.find().skip(2)
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag", "profession" : "Driver" }

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

Example 2 – MongoDB skip() with no argument

If no integer is provided as argument to skip() method, no documents will be skipped in the result.

> db.people.find().skip()
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb2"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag", "profession" : "Driver" }

All documents are fetched in the result.

Example 3 – MongoDB skip() with limit() for pagination

In this example, we will use skip() method with limit() method. We shall skip one document and limit to two documents in the result.

> db.people.find().skip(1).limit(2)
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" }

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

For page-based results, the usual formula is:

</>
Copy
skipValue = (pageNumber - 1) * pageSize

For example, if each page has 10 documents, page 1 uses skip(0).limit(10), page 2 uses skip(10).limit(10), and page 3 uses skip(20).limit(10).

</>
Copy
db.people.find().skip(20).limit(10)

MongoDB sort(), skip(), and limit() for stable page results

When using skip() for pagination, add sort() so that results appear in a stable order. Without a sort order, the database may return documents in an order that is not meaningful for your page logic.

The following query sorts documents by _id in ascending order, skips the first two documents, and returns the next two documents.

</>
Copy
db.people.find().sort({ _id: 1 }).skip(2).limit(2)

You can sort by any field that fits the application requirement, such as name, age, createdAt, or a score field. For larger collections, prefer an index that supports the filter and sort fields used in the query.

MongoDB skip() with a filter condition

skip() can be used after a filter condition. The following query finds people whose age is greater than or equal to 24, sorts them by age, skips the first matching document, and returns the next two documents.

</>
Copy
db.people.find({ age: { $gte: 24 } }).sort({ age: 1 }).skip(1).limit(2)

In this query, the filter reduces the matching set, sort() orders that set, skip(1) moves past the first matching document, and limit(2) returns two documents from the remaining cursor.

MongoDB aggregation $skip stage

MongoDB also provides the $skip stage for aggregation pipelines. Use $skip when you are working inside aggregate() instead of a normal find() cursor.

</>
Copy
db.people.aggregate([
  { $match: { age: { $gte: 24 } } },
  { $sort: { age: 1 } },
  { $skip: 1 },
  { $limit: 2 }
])

In an aggregation pipeline, stage order matters. Place $match and $sort before $skip when you want to skip documents from a filtered and ordered result set. If $skip is placed too early, later stages will process only the documents that remain after the skip.

MongoDB skip() performance and deep pagination

skip() is simple and works well for small offsets. However, when the skip value becomes very large, the database still has to move through the skipped documents before returning the requested page. This can make deep pagination slower on large collections.

For large datasets, a range-based pagination pattern is often better. Instead of skipping thousands of records, use the last seen value from the previous page and query the next range. The following example fetches documents after a known _id.

</>
Copy
db.people.find({ _id: { $gt: lastSeenId } }).sort({ _id: 1 }).limit(10)

This approach is useful when users move forward through a long list, such as activity feeds, logs, or large search result sets. Page-number pagination is easier to implement, but range-based pagination can be more efficient for deeper pages.

Common mistakes with MongoDB skip documents

  • Using skip without sort for pagination: Add sort() so that page results remain predictable.
  • Assuming skip changes the collection: skip() only changes which documents are returned by the cursor. It does not update, delete, or move stored documents.
  • Confusing cursor skip and aggregation skip: Use cursor.skip(N) with find() and { $skip: N } inside aggregate().
  • Skipping too many records on large collections: High skip values can be inefficient. Consider range-based pagination when deep pages are common.
  • Forgetting indexes on sorted pagination fields: If the query filters or sorts a large collection, use suitable indexes for those fields.

QA checklist for MongoDB skip() examples

  • Verify that the skip value is a non-negative integer used to move the cursor forward.
  • Use sort() in pagination examples so that skipped and returned documents are predictable.
  • Check that the page formula (pageNumber - 1) * pageSize matches the example values.
  • Use limit() with skip() when the page should return a fixed number of documents.
  • Use $skip instead of skip() when the example is written as an aggregation pipeline.

FAQs on MongoDB skip documents

What does skip() do in MongoDB?

skip() skips the specified number of documents from the beginning of a cursor result. For example, db.people.find().skip(2) skips the first two documents returned by the cursor.

How do I use skip() and limit() together in MongoDB?

Use skip() to move past earlier documents and limit() to control how many documents are returned. For example, db.people.find().skip(10).limit(10) can be used for the second page when the page size is 10.

Should MongoDB skip() be used with sort()?

Yes, use sort() when the order matters, especially in pagination. A stable sort makes sure the same page request returns documents in a predictable order.

What is the difference between skip() and $skip in MongoDB?

skip() is a cursor method used with find(). $skip is an aggregation pipeline stage used inside aggregate().

Is MongoDB skip() good for large page numbers?

skip() is convenient for small offsets, but very large skip values can become slower on large collections. For deep pagination, consider range-based pagination using an indexed field such as _id or createdAt.

Conclusion

In this MongoDB tutorialMongoDB Skip Documents, we have learnt to skip first N number of documents that a query returns in the result. Use db.collection.find().skip(N) for regular cursor queries, combine it with limit() for simple pagination, and add sort() when page order matters. For aggregation pipelines, use the $skip stage. In our next tutorial, we shall learn to sort documents.