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.
db.collection.find().sort({age: 1}).skip(3).limit(2)| Step | Action | Documents Before | Documents After | Notes |
|---|---|---|---|---|
| 1 | Start 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 |
| 2 | Sort 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 |
| 3 | Skip first 3 documents | [{age: 20}, {age: 25}, {age: 30}, {age: 35}, {age: 40}] | [{age: 35}, {age: 40}] | Documents with age 20, 25, 30 skipped |
| 4 | Limit to 2 documents | [{age: 35}, {age: 40}] | [{age: 35}, {age: 40}] | Only 2 documents returned |
| 5 | End | [{age: 35}, {age: 40}] | [{age: 35}, {age: 40}] | Query complete |
| Variable | Start | After Sort | After Skip | After Limit | Final |
|---|---|---|---|---|---|
| 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}] |
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.