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.

CategoryCursor
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.find(<filter>).batchSize(<size>)

⚙️ Parameters

countDocuments 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏