batchSize() (Set Batch Fetch Size)
Last updated: 2026-10-09
batchSize() (Set Batch Fetch Size)
batchSize() sets how many documents the cursor pulls from the server per batch: `db.users.find().batchSize(100)`. It trades round trips against memory: larger batches mean fewer requests but heftier per-batch memory. Usually the server's default suffices; tuning batchSize helps throughput on slow links or huge result sets.
| Category | Cursor |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.find(<filter>).batchSize(<size>)
⚙️ Parameters
| count | Documents per batch; 0 restores the default. |
|---|
Returns:The cursor with the batch size applied (chainable).
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// batchSize: fetch 50 per batch, process as it streams
const cursor = db.users.find().batchSize(50)
cursor.forEach(doc => print(doc.name))
Output:
Alice
Bob
Cara
📌 Notes
- 0 restores the server default
- Oversized batches inflate per-transfer memory
- Pairs best with forEach() streaming
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat batchSize should I use?
AThe default is usually fine; bump it on fast networks for fewer round trips, lower it when memory is tight.
QHow does batchSize differ from limit?
Alimit() caps the total documents returned; batchSize() only controls transfer chunk size, leaving the total unchanged.
QWhat does batchSize(0) mean?
A0 restores the server's default batch size.