Bird
Raised Fist0
MongoDBquery~10 mins

Pagination pattern with skip and limit in MongoDB - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Pagination pattern with skip and limit
Start Query
Apply skip(n)
Apply limit(m)
Return m documents starting from n+1
Display Page Results
User requests next page?
YesIncrease skip by m
No
End
The query skips a number of documents, then limits the result to a page size, returning that page's documents. User can request next page by increasing skip.
Execution Sample
MongoDB
db.collection.find().skip(5).limit(3)
This query skips the first 5 documents and returns the next 3 documents as a page.
Execution Table
StepActionDocuments SkippedDocuments ReturnedResulting Documents
1Start query on collection with 10 documents00[doc1, doc2, doc3, doc4, doc5, doc6, doc7, doc8, doc9, doc10]
2Apply skip(5)50[doc6, doc7, doc8, doc9, doc10]
3Apply limit(3)53[doc6, doc7, doc8]
4Return results53[doc6, doc7, doc8]
5User requests next page, increase skip to 880[doc9, doc10]
6Apply limit(3) on new skip82[doc9, doc10]
7Return results for next page82[doc9, doc10]
8No more documents to fetch, end pagination100[]
💡 No more documents after skipping 10, so pagination ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
skip0558810
limit333333
documents_skipped0558810
documents_returned003020
resulting_documents[doc1..doc10][doc6..doc10][doc6, doc7, doc8][doc9, doc10][doc9, doc10][]
Key Moments - 3 Insights
Why does the query skip documents before limiting?
Because skip(n) moves the cursor past the first n documents, so limit(m) returns the next m documents after skipping. See execution_table rows 2 and 3.
What happens if skip is larger than the total documents?
No documents are returned because the cursor moves past all documents. See execution_table row 8 where skip=10 returns empty.
Why does increasing skip by limit give the next page?
Because each page shows 'limit' documents, increasing skip by that amount moves to the next set. See execution_table rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what documents are returned at Step 3?
A[doc1, doc2, doc3]
B[doc9, doc10]
C[doc6, doc7, doc8]
D[]
💡 Hint
Check the 'Resulting Documents' column at Step 3 in the execution_table.
At which step does the condition 'no more documents to fetch' become true?
AStep 4
BStep 8
CStep 7
DStep 2
💡 Hint
Look for the step where the resulting documents are empty in the execution_table.
If limit was changed to 2, how many documents would be returned at Step 3?
A2
B3
C5
D0
💡 Hint
Refer to variable_tracker for 'limit' and how it affects documents returned.
Concept Snapshot
Pagination with skip and limit:
- Use skip(n) to skip first n documents
- Use limit(m) to get next m documents
- Combine to get pages: skip = page_number * m
- If skip > total docs, returns empty
- Good for small pages, but large skip can be slow
Full Transcript
This visual execution shows how MongoDB pagination works using skip and limit. The query first skips a number of documents, then limits the output to a page size. For example, skip(5) and limit(3) returns documents 6, 7, and 8. Increasing skip by the limit value fetches the next page. If skip exceeds the total documents, no results are returned, ending pagination. This pattern helps show data page by page.

Practice

(1/5)
1.

What does the skip method do in MongoDB pagination?

easy
A. It limits the number of documents returned.
B. It deletes documents from the collection.
C. It sorts the documents in ascending order.
D. It skips a specified number of documents before returning results.

Solution

  1. Step 1: Understand the role of skip

    The skip method tells MongoDB to ignore a certain number of documents from the start of the result set.
  2. Step 2: Compare with other methods

    limit controls how many documents to return, not skipping. Sorting and deleting are unrelated to skip.
  3. Final Answer:

    It skips a specified number of documents before returning results. -> Option D
  4. Quick Check:

    skip = skip documents [OK]
Hint: Skip jumps over documents, limit sets how many to show [OK]
Common Mistakes:
  • Confusing skip with limit
  • Thinking skip sorts documents
  • Assuming skip deletes documents
2.

Which of the following is the correct syntax to get the second page of results with 5 documents per page in MongoDB?

db.collection.find().skip(?).limit(?)
easy
A. skip(1).limit(5)
B. skip(10).limit(5)
C. skip(5).limit(5)
D. skip(0).limit(5)

Solution

  1. Step 1: Calculate skip value for page 2

    Each page has 5 documents, so page 1 skips 0, page 2 skips 5 documents (5 * (2-1) = 5).
  2. Step 2: Set limit to page size

    Limit should be 5 to get 5 documents per page.
  3. Final Answer:

    skip(5).limit(5) -> Option C
  4. Quick Check:

    Page 2 skip = 5, limit = 5 [OK]
Hint: Skip = pageSize * (pageNumber - 1), limit = pageSize [OK]
Common Mistakes:
  • Using skip(10) for page 2
  • Setting skip to 1 instead of 5
  • Confusing skip and limit values
3.

Given a collection with documents numbered 1 to 10, what documents will this query return?

db.collection.find().skip(3).limit(4)
medium
A. Documents 1 to 4
B. Documents 4 to 7
C. Documents 3 to 6
D. Documents 5 to 8

Solution

  1. Step 1: Understand skip(3)

    Skip the first 3 documents: documents 1, 2, and 3 are ignored.
  2. Step 2: Apply limit(4)

    Return the next 4 documents after skipping: documents 4, 5, 6, and 7.
  3. Final Answer:

    Documents 4 to 7 -> Option B
  4. Quick Check:

    Skip 3, then limit 4 = docs 4-7 [OK]
Hint: Skip removes first N, limit picks next M documents [OK]
Common Mistakes:
  • Counting skip as inclusive
  • Starting limit count before skip
  • Off-by-one errors in document numbers
4.

Identify the error in this pagination query to get page 3 with 10 documents per page:

db.collection.find().skip(20).limit(20)
medium
A. limit should be 10, not 20
B. skip value is too high
C. skip should be 30 for page 3
D. No error, query is correct

Solution

  1. Step 1: Calculate correct skip and limit for page 3

    Skip should be 10 * (3-1) = 20, which is correct here.
  2. Step 2: Check limit value

    Limit should be 10 to get 10 documents per page, but query uses limit(20), which is too many.
  3. Final Answer:

    limit should be 10, not 20 -> Option A
  4. Quick Check:

    Limit = page size = 10 [OK]
Hint: Limit equals page size, skip = pageSize*(page-1) [OK]
Common Mistakes:
  • Setting limit equal to skip
  • Using skip as page number
  • Assuming no limit needed
5.

You want to display page 4 of a product list with 8 items per page. However, the total products are 30. Which query correctly handles this pagination without returning empty results?

db.products.find().skip(?).limit(?)
hard
A. skip(24).limit(8)
B. skip(32).limit(8)
C. skip(16).limit(8)
D. skip(30).limit(8)

Solution

  1. Step 1: Calculate skip for page 4

    Skip = 8 * (4-1) = 24 documents to reach page 4.
  2. Step 2: Check total documents and limit

    Total is 30, so skipping 24 leaves 6 documents (30-24). Limit 8 is okay; it will return only available 6 documents without error.
  3. Step 3: Analyze other options

    skip(32) and skip(30) exceed total documents, returning empty results. skip(16) is for page 3, not page 4.
  4. Final Answer:

    skip(24).limit(8) -> Option A
  5. Quick Check:

    Page 4 skip = 24, limit = 8, total 30 [OK]
Hint: Skip = pageSize*(page-1), limit = pageSize, check total docs [OK]
Common Mistakes:
  • Skipping beyond total documents
  • Using wrong skip for page number
  • Ignoring total document count