In MongoDB, limit() and skip()
help control how many documents a query returns and where
the returned results begin. They are especially useful when
displaying records page by page.
The limit() method restricts the maximum
number of documents returned by a query.
db.collection.find().limit(number)
db.students.find().limit(3)
This returns at most three documents from the
students collection.
db.products.find().limit(5)
This returns at most five product documents.
The skip() method skips a specified
number of documents before returning the remaining
query results.
db.collection.find().skip(number)
db.students.find().skip(3)
This skips the first three documents in the query results and returns the remaining documents.
db.students.find().skip(5)
This skips the first five results.
We can combine both methods to skip some documents and then return a limited number of results.
db.students.find()
.skip(3)
.limit(5)
This skips three documents and returns up to five documents from the remaining results.
db.products.find()
.skip(10)
.limit(5)
This skips ten results and returns up to five product documents.
Combining sort() and
limit() helps retrieve the highest,
lowest, newest, or oldest records.
db.employees.find()
.sort({ salary: -1 })
.limit(3)
This returns up to three employees with the highest salaries.
db.products.find()
.sort({ price: 1 })
.limit(2)
This returns up to two products with the lowest prices.
Pagination means dividing a large set of records into smaller pages. For example, a website might display ten students per page.
Suppose each page contains 10 documents:
db.students.find()
.sort({ _id: 1 })
.skip(0)
.limit(10)
db.students.find()
.sort({ _id: 1 })
.skip(10)
.limit(10)
db.students.find()
.sort({ _id: 1 })
.skip(20)
.limit(10)
Each page skips the documents displayed on the previous pages. Sorting by a stable, unique field helps keep pagination results consistent.
To calculate how many documents to skip for a page, use this formula:
skip = (pageNumber - 1) * pageSize
For example, if each page contains 10 documents:
| Page Number | Page Size | Skip Value |
|---|---|---|
| 1 | 10 | 0 |
| 2 | 10 | 10 |
| 3 | 10 | 20 |
| 4 | 10 | 30 |
| 5 | 10 | 40 |
db.students.find()
.sort({ _id: 1 })
.skip((4 - 1) * 10)
.limit(10)
The skip value is 30, so MongoDB skips 30 documents and returns up to the next 10.
We can filter documents first, sort the matching results, and then use pagination.
db.students.find({
course: "Python"
})
.sort({ _id: 1 })
.skip(5)
.limit(5)
This finds Python students, sorts them by
_id, skips the first five matching
results, and returns up to five more.
db.users.find({
status: "active"
})
.sort({ _id: 1 })
.limit(10)
This returns up to ten active users in the selected order.
limit(n) returns at most n documents.skip(n) skips n documents in the query results.sort() for predictable pagination.skip = (pageNumber - 1) * pageSize to calculate pagination offsets.
Create a collection named practice_pagination
and insert these sample documents:
db.practice_pagination.insertMany([
{ _id: 1, name: "Rahul", age: 22, fee: 5000 },
{ _id: 2, name: "Amit", age: 19, fee: 6000 },
{ _id: 3, name: "Priya", age: 21, fee: 4500 },
{ _id: 4, name: "Neha", age: 20, fee: 7000 },
{ _id: 5, name: "Aman", age: 23, fee: 5500 },
{ _id: 6, name: "Riya", age: 18, fee: 4000 },
{ _id: 7, name: "Karan", age: 24, fee: 6500 },
{ _id: 8, name: "Pooja", age: 20, fee: 4800 }
])
limit() and skip().Run the insertion command only once, or clear the test collection before inserting the same sample data again.
1. What does limit(5) do?
2. What does skip(10) do?
3. What is the skip value for page 3 if each page contains 10 documents?