limit() (Limit Result Count)
Last updated: 2026-10-09
limit() (Limit Result Count)
limit() caps how many documents find() returns: `db.users.find({ age: { $gt: 20 } }).limit(5)`. It chains onto the cursor and is the standard tool for pagination and top-N queries. limit(0) means no limit; it composes freely with other cursor methods, e.g. skip() for paging.
| Category | Cursor |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.find(<filter>).limit(<count>)
⚙️ Parameters
| count | Maximum documents to return; 0 means unlimited. |
|---|
Returns:The cursor with the limit applied (chainable).
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// limit: take only the first 3 adults
const top3 = db.users.find({ age: { $gte: 18 } }).limit(3).toArray()
top3
Output:
[
{ _id: ObjectId('64c8a1b20000000000000001'), name: 'Alice', age: 21 },
{ _id: ObjectId('64c8a1b20000000000000002'), name: 'Bob', age: 25 }
]
📌 Notes
- limit() truncates server-side; combined with skip() it is not a globally consistent paginator
- limit(0) means unlimited
- Pair with sort for predictability, e.g. .sort({age: 1}).limit(5)
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do limit and skip combine for pagination?
ATypical pattern: .skip(pageSize*page).limit(pageSize), e.g. .skip(10).limit(10) for page two of ten.
QWhat does limit(0) mean?
Alimit(0) applies no limit, equivalent to omitting it.
QCan I chain more cursor methods after limit?
AYes — limit() returns a cursor, so sort(), skip(), toArray(), etc. can follow.