MongoDB Query to Find with Pagination Example
Use
db.collection.find().skip(pageSize * (pageNumber - 1)).limit(pageSize) to find documents with pagination in MongoDB, where pageSize is items per page and pageNumber is the current page.Examples
InputpageSize = 2, pageNumber = 1
Output[{_id:1, name:'Alice'}, {_id:2, name:'Bob'}]
InputpageSize = 2, pageNumber = 2
Output[{_id:3, name:'Carol'}, {_id:4, name:'Dave'}]
InputpageSize = 3, pageNumber = 4 (no more data)
Output[]
How to Think About It
To paginate results, first decide how many items you want per page (pageSize) and which page you want (pageNumber). Then skip the items from previous pages using
skip and limit the results to pageSize using limit. This way, you get only the items for the current page.Algorithm
1
Get the page size (number of items per page) and the page number (which page to show).2
Calculate how many items to skip: (pageNumber - 1) * pageSize.3
Run the find query on the collection.4
Use skip with the calculated number to ignore previous pages.5
Use limit with pageSize to get only the current page items.6
Return the results.Code
mongodb
const pageSize = 2; const pageNumber = 2; const results = db.users.find() .skip(pageSize * (pageNumber - 1)) .limit(pageSize) .toArray(); printjson(results);
Output
[
{"_id": 3, "name": "Carol"},
{"_id": 4, "name": "Dave"}
]
Dry Run
Let's trace pageSize=2 and pageNumber=2 through the query.
1
Calculate skip
skip = 2 * (2 - 1) = 2
2
Run find with skip and limit
Skip first 2 documents, then limit to 2 documents
3
Return results
Documents with _id 3 and 4
| Operation | Value |
|---|---|
| pageSize | 2 |
| pageNumber | 2 |
| skip | 2 |
| limit | 2 |
| returned documents | [{_id:3, name:'Carol'}, {_id:4, name:'Dave'}] |
Why This Works
Step 1: Why use skip?
The skip operator ignores documents from earlier pages so you start at the right place.
Step 2: Why use limit?
The limit operator restricts the number of documents returned to the page size.
Step 3: Combining skip and limit
Together, skip and limit let you get just the documents for the current page.
Alternative Approaches
Using range queries with _id for pagination
mongodb
const lastId = ObjectId('someId'); const pageSize = 2; const results = db.users.find({_id: {$gt: lastId}}) .limit(pageSize) .toArray(); printjson(results);
This method is faster for large collections but requires tracking the last _id from the previous page.
Using aggregation with $skip and $limit
mongodb
const pageSize = 2; const pageNumber = 2; const results = db.users.aggregate([ { $skip: pageSize * (pageNumber - 1) }, { $limit: pageSize } ]).toArray(); printjson(results);
Aggregation can be more flexible if you want to add sorting or filtering in the same pipeline.
Complexity: O(n) time, O(k) space
Time Complexity
The skip operation requires scanning and discarding documents, so time grows with the skip amount, making it O(n) where n is skip count.
Space Complexity
Only the pageSize number of documents are stored in memory, so space is O(k) where k is pageSize.
Which Approach is Fastest?
Using range queries with _id is faster for large skips because it avoids scanning skipped documents.
| Approach | Time | Space | Best For |
|---|---|---|---|
| skip & limit | O(n) | O(k) | Simple pagination, small skips |
| range query with _id | O(k) | O(k) | Large collections, fast pagination |
| aggregation pipeline | O(n) | O(k) | Complex queries with filtering/sorting |
Always sort your results before paginating to get consistent pages.
Forgetting to subtract 1 from pageNumber when calculating skip causes wrong pages.