Lesson 19 of 50 – Limit and Skip in MongoDB
38%

Limit and Skip in MongoDB

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.

1. What Is limit()?

The limit() method restricts the maximum number of documents returned by a query.

Syntax
db.collection.find().limit(number)
Example
db.students.find().limit(3)

This returns at most three documents from the students collection.

Another Example
db.products.find().limit(5)

This returns at most five product documents.

2. What Is skip()?

The skip() method skips a specified number of documents before returning the remaining query results.

Syntax
db.collection.find().skip(number)
Example
db.students.find().skip(3)

This skips the first three documents in the query results and returns the remaining documents.

Another Example
db.students.find().skip(5)

This skips the first five results.

3. Using limit() and skip() Together

We can combine both methods to skip some documents and then return a limited number of results.

Example
db.students.find()
    .skip(3)
    .limit(5)

This skips three documents and returns up to five documents from the remaining results.

Another Example
db.products.find()
    .skip(10)
    .limit(5)

This skips ten results and returns up to five product documents.

4. Using limit() with sort()

Combining sort() and limit() helps retrieve the highest, lowest, newest, or oldest records.

Example: Three Highest-Paid Employees
db.employees.find()
    .sort({ salary: -1 })
    .limit(3)

This returns up to three employees with the highest salaries.

Example: Two Cheapest Products
db.products.find()
    .sort({ price: 1 })
    .limit(2)

This returns up to two products with the lowest prices.

5. Pagination in MongoDB

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:

Page 1
db.students.find()
    .sort({ _id: 1 })
    .skip(0)
    .limit(10)
Page 2
db.students.find()
    .sort({ _id: 1 })
    .skip(10)
    .limit(10)
Page 3
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.

6. Calculate the Skip Value

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
Example: Display Page 4
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.

7. Using limit() and skip() with a Filter

We can filter documents first, sort the matching results, and then use pagination.

Example
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.

Example: Active Users
db.users.find({
    status: "active"
})
.sort({ _id: 1 })
.limit(10)

This returns up to ten active users in the selected order.

8. Important Points to Remember

  • limit(n) returns at most n documents.
  • skip(n) skips n documents in the query results.
  • Both methods can be combined.
  • Use sort() for predictable pagination.
  • Use skip = (pageNumber - 1) * pageSize to calculate pagination offsets.
  • Large skip values can be slow on large collections.
  • For very large datasets, range-based pagination may perform better.

9. Practical Assignment

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 }
])
Complete these tasks:
  1. Display only the first three students.
  2. Skip the first two students and display the remaining results.
  3. Skip two students and return the next three.
  4. Display the three students with the highest fees.
  5. Display the two youngest students.
  6. Display page 2 with three students per page.
  7. Find students whose age is greater than 20 and return only two results.
  8. Explain the difference between limit() and skip().

Run the insertion command only once, or clear the test collection before inserting the same sample data again.

🧠 Quick Quiz

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?