0PricingLogin
MongoDB Academy · Lesson

Skip and Limit: Offset Pagination

Learners will implement traditional page-number pagination with skip() and limit(), and measure its performance cost on large collections.

What Is Offset Pagination?

Offset pagination—also called page-number pagination—divides results into fixed-size pages and uses a page number to determine how far into the result set to start. Page 1 shows items 1-20, page 2 shows items 21-40, and so on. In MongoDB this is implemented with skip() to jump over earlier results and limit() to restrict how many documents are returned per page.

Using skip() and limit()

limit(N) tells the cursor to return at most N documents. skip(N) tells MongoDB to skip the first N documents before returning any results. Together they implement page-number pagination: to get page P with size S items per page, use skip((P-1)*S) and limit(S).

const PAGE = 3;
const PAGE_SIZE = 20;

// Page 3 of 20 results per page
const products = await db.collection('products')
  .find({ isActive: true })
  .sort({ createdAt: -1 })
  .skip((PAGE - 1) * PAGE_SIZE)  // skip 40 docs (pages 1 and 2)
  .limit(PAGE_SIZE)               // return next 20
  .toArray();

console.log('Page 3 results:', products.length);

All lessons in this course

  1. Sorting With sort() and Multiple Keys
  2. Skip and Limit: Offset Pagination
  3. Keyset Pagination With Range Queries
  4. Combining Sort, Skip, Limit, and Projections
← Back to MongoDB Academy