← Back to MongoDB Course | Chapter 3: CRUD — Read | Lesson 5 of 7

limit/skip

limit caps how many documents return and skip jumps over the first ones, which together give pagination.

In this page:

  1. limit/skip
Syntax
javascript
db.collection_name.find(filter).skip(n).limit(n)

limit/skip

cursor.limit(n) returns at most n documents and cursor.skip(n) omits the first n. Combine them with sort for stable pages. Large skips are slow because MongoDB still walks the skipped documents; range queries on an indexed field scale better.

Note: Always sort before paging so pages stay consistent.

Example: limit/skip

bash
test> db.items.insertMany([1, 2, 3, 4, 5, 6, 7].map((n) => ({ _id: n, label: "item" + n })))
{
  acknowledged: true,
  insertedIds: { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6, '6': 7 }
}
test> db.items.find().sort({ _id: 1 }).limit(3)
[
  { _id: 1, label: 'item1' },
  { _id: 2, label: 'item2' },
  { _id: 3, label: 'item3' }
]
test> db.items.find().sort({ _id: 1 }).skip(3).limit(3)
[
  { _id: 4, label: 'item4' },
  { _id: 5, label: 'item5' },
  { _id: 6, label: 'item6' }
]
test> db.items.find().sort({ _id: -1 }).limit(2)
[ { _id: 7, label: 'item7' }, { _id: 6, label: 'item6' } ]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Paging without sort
  2. Using huge skip values
  3. Forgetting limit applies after sort
Chapter Summary
  • limit caps the result size
  • skip omits leading documents
  • Sort first for stable pages
  • Prefer range paging for big collections
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.