Introduction
When a MongoDB collection contains a large number of documents, displaying every document at once is not practical. MongoDB provides the skip() method to skip a specific number of documents and limit() to control how many documents are returned. By combining skip() and limit(), we can create pagination, where data is displayed page by page. In this chapter, you will practice skip(), limit(), and pagination with practical MongoDB queries. MongoDB Skip and Pagination practice questions with solutions to help you understand the concepts.
Q1. Skip the First 3 Documents
Problem Statement
Suppose the students collection contains student records. Write a MongoDB query to skip the first 3 documents.
MongoDB Query
db.students.find().skip(3)
Expected Output
The first 3 documents will not be displayed. MongoDB will return the documents starting from the 4th document.
Explanation
The skip() method tells MongoDB how many documents should be skipped before returning results.
.skip(3)
means skip 3 documents.
Q2. Return Only the First 5 Documents
Problem Statement
Write a MongoDB query to display only the first 5 students.
MongoDB Query
db.students.find().limit(5)
Expected Output
Student 1
Student 2
Student 3
Student 4
Student 5
Explanation
The limit() method restricts the number of documents returned.
.limit(5)
means return a maximum of 5 documents.
Q3. Skip 5 Documents and Return the Next 5
Problem Statement
Write a query that skips the first 5 students and returns the next 5 students.
MongoDB Query
db.students.find().skip(5).limit(5)
Expected Output
Student 6
Student 7
Student 8
Student 9
Student 10
Explanation
MongoDB first skips 5 documents:
.skip(5)
Then it returns a maximum of 5 documents:
.limit(5)
This is one of the basic building blocks of pagination.
Q4. Display the Second Page
Problem Statement
You want to display students in groups of 10. Write a query to display page 2.
MongoDB Query
db.students.find().skip(10).limit(10)
Expected Output
Student 11
Student 12
Student 13
Student 14
Student 15
Student 16
Student 17
Student 18
Student 19
Student 20
Explanation
If each page contains 10 documents:
- Page 1 → skip
0, limit10 - Page 2 → skip
10, limit10 - Page 3 → skip
20, limit10
Therefore, page 2 uses:
.skip(10).limit(10)
Q5. Display the Third Page
Problem Statement
Each page should contain 5 documents. Write a query to display page 3.
MongoDB Query
db.students.find().skip(10).limit(5)
Expected Output
Student 11
Student 12
Student 13
Student 14
Student 15
Explanation
The pagination formula is:
Skip = (Page Number - 1) × Documents Per Page
For page 3:
Skip = (3 - 1) × 5
= 10
Therefore:
db.students.find().skip(10).limit(5)
Q6. Create Pagination Using Variables
Problem Statement
Create a simple pagination query where the page number is 4 and each page contains 10 documents.
MongoDB Query
let page = 4;
let limit = 10;
let skip = (page - 1) * limit;
db.students.find().skip(skip).limit(limit);
Expected Output
Student 31
Student 32
Student 33
...
Student 40
Explanation
The skip value is calculated using:
(page - 1) * limit
For page 4:
(4 - 1) × 10 = 30
So MongoDB skips 30 documents and returns the next 10.
Q7. Use Skip and Limit with Sorting
Problem Statement
Display the second page of students, with 5 students per page, sorted by name in ascending order.
MongoDB Query
db.students
.find()
.sort({ name: 1 })
.skip(5)
.limit(5)
Expected Output
The first 5 students alphabetically will be skipped, and the next 5 students will be displayed.
Explanation
Here, the operations are:
.sort({ name: 1 })
Sort names in ascending order.
.skip(5)
Skip the first 5 sorted documents.
.limit(5)
Return the next 5 documents.
Tip: For predictable pagination, especially when multiple documents can have the same sort value, include a unique secondary sort field such as _id.
Q8. Paginate Filtered Results
Problem Statement
Find students from the Delhi city and display the second page, with 5 students per page.
MongoDB Query
db.students
.find({ city: "Delhi" })
.skip(5)
.limit(5)
Expected Output
The first 5 Delhi students will be skipped, and the next 5 matching students will be returned.
Explanation
The filter is applied first:
{ city: "Delhi" }
Then pagination is applied:
.skip(5).limit(5)
This allows you to paginate only the documents matching a particular condition.
Q9. Find the Number of Pages
Problem Statement
Suppose there are 47 students and you want to display 10 students per page. How many pages are required?
MongoDB Query
MongoDB can count the documents:
db.students.countDocuments()
Suppose the result is:
47
You can calculate the number of pages in JavaScript:
let totalStudents = db.students.countDocuments();
let studentsPerPage = 10;
let totalPages = Math.ceil(totalStudents / studentsPerPage);
print(totalPages);
Expected Output
5
Explanation
There are 47 students:
47 ÷ 10 = 4.7
Since you cannot have 0.7 of a page, the result is rounded upward:
5 pages
The Math.ceil() function is useful for calculating the total number of pagination pages.
Q10. Create a Complete Pagination Query
Problem Statement
Create a reusable pagination query where:
- Page number = 3
- Documents per page = 10
- Students are sorted by name
- Only students from Delhi should be displayed
MongoDB Query
let page = 3;
let limit = 10;
let skip = (page - 1) * limit;
db.students
.find({ city: "Delhi" })
.sort({ name: 1, _id: 1 })
.skip(skip)
.limit(limit);
Expected Output
The query returns up to 10 Delhi students from page 3, ordered by name.
Explanation
The pagination calculation is:
(3 - 1) × 10 = 20
So MongoDB skips the first 20 matching documents and returns the next 10.
The query combines several MongoDB concepts:
find()
sort()
skip()
limit()
This is a common pattern for implementing basic page-based pagination.
Key Takeaways
skip()in MongoDB is used to skip a specific number of documents.limit()in MongoDB controls the maximum number of documents returned.skip()in MongoDB andlimit()in MongoDB can be combined to create pagination.- The basic pagination formula is
(page - 1) × limit. sort()in MongoDB should generally be used when implementing predictable page-based results.- A unique secondary sort field such as
_idcan make ordering more stable. countDocuments()can be used to determine the total number of matching documents.Math.ceil()can help calculate the total number of pages.- Pagination can also be applied after filtering with
find(). - For very large datasets,
skip()-based pagination can become inefficient; range/cursor-based pagination is often a better advanced approach.
FAQs
1. What is skip() in MongoDB?
skip() is a MongoDB cursor method that skips a specified number of documents before returning results.
2. What is limit() in MongoDB?
limit() restricts the maximum number of documents returned by a query.
3. How do I create pagination in MongoDB?
A basic pagination query can use:
db.students.find().skip((page - 1) * limit).limit(limit)
4. How do I get page 2 in MongoDB?
If each page contains 10 documents:
db.students.find().skip(10).limit(10)
5. Can I use skip() and limit() with sort() in MongoDB?
Yes. For example:
db.students
.find()
.sort({ name: 1 })
.skip(10)
.limit(10)
6. How can I calculate the total number of pages in MongoDB pagination?
First count the matching documents using countDocuments(), then divide by the page size and round upward:
Math.ceil(totalDocuments / documentsPerPage)
7. Is skip() good for very large MongoDB collections?
skip() is simple and useful for basic pagination, but skipping large numbers of documents can become inefficient. For large datasets, range-based or cursor-based pagination is generally more scalable.
8. What is the use of sort() in MongoDB?
MongoDB’s sort() method is used to arrange query results in ascending or descending order. Use 1 for ascending order and -1 for descending order.
db.students.find().sort({ age: 1 })
This displays students from youngest to oldest.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
