0
0
MongoDBquery~10 mins

skip method for offset in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - skip method for offset
Start Query
Apply Filter
Sort Documents
Skip N Documents
Return Remaining Documents
End
The skip method tells MongoDB to ignore a number of documents at the start of the result, then return the rest.
Execution Sample
MongoDB
db.collection.find().sort({age: 1}).skip(3).limit(2)
This query sorts documents by age ascending, skips the first 3, then returns the next 2 documents.
Execution Table
StepActionDocuments BeforeDocuments AfterNotes
1Start with all documents[{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}][{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}]Initial collection
2Sort by age ascending[{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}][{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}]Already sorted in this example
3Skip first 3 documents[{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}][{age: 35}, {age: 40}]Documents with age 20, 25, 30 skipped
4Limit to 2 documents[{age: 35}, {age: 40}][{age: 35}, {age: 40}]Only 2 documents returned
5End[{age: 35}, {age: 40}][{age: 35}, {age: 40}]Query complete
💡 After skipping 3 documents, only documents from index 3 onward are returned, limited to 2.
Variable Tracker
VariableStartAfter SortAfter SkipAfter LimitFinal
documents[{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}][{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}][{age: 35}, {age: 40}][{age: 35}, {age: 40}][{age: 35}, {age: 40}]
Key Moments - 3 Insights
Why does skip(3) remove the first three documents instead of the last three?
skip(3) tells MongoDB to ignore the first 3 documents in the sorted list, so it removes documents at the start, not the end. See execution_table row 3 where the first three documents are skipped.
What happens if skip number is larger than total documents?
If skip is larger than the number of documents, no documents remain to return. The result will be empty. This is because all documents are skipped.
Does skip change the order of documents?
No, skip only removes documents from the start after sorting. The order of remaining documents stays the same. See execution_table rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what documents remain after the skip step (Step 3)?
A[{age: 20}, {age: 25}, {age: 30}]
B[{age: 35}, {age: 40}]
C[{age: 25}, {age: 30}, {age: 35}]
D[{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}]
💡 Hint
Check the 'Documents After' column at Step 3 in the execution_table.
At which step does the query limit the number of documents returned?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Limit to 2 documents' in the execution_table.
If skip(0) was used instead of skip(3), how would the documents after skip change at Step 3?
AAll documents would remain, same as before skip
BOnly first 3 documents would remain
CNo documents would remain
DOnly last 2 documents would remain
💡 Hint
skip(0) means skip zero documents, so no documents are removed at Step 3.
Concept Snapshot
skip(n) in MongoDB skips the first n documents in the query result.
Use it after sorting to offset results.
Commonly combined with limit() to paginate.
If n is larger than total docs, result is empty.
Order of remaining docs stays unchanged.
Full Transcript
The skip method in MongoDB is used to ignore a number of documents at the start of the query result. First, the query finds all documents and applies any filters. Then it sorts the documents if specified. After sorting, skip(n) removes the first n documents from the result. The remaining documents are returned, often limited by limit(). For example, if we have documents with ages 20, 25, 30, 35, and 40, and we skip 3, the first three documents (ages 20, 25, 30) are ignored. The query then returns documents starting from age 35 onward. This is useful for pagination to show results starting from a certain offset. If skip is zero, no documents are skipped. If skip is larger than the number of documents, the result is empty. The order of documents after skipping remains the same as after sorting.