A mongoose plugin that allows iterations over batches with cursor.
$ npm i --save mongoose-cursorbatched
batchSize: Number of records per batch (default: 20)
const mongoose = require('mongoose');
const cursorbatched = require('mongoose-cursorbatched');
mongoose.plugin(cursorbatched);
mongoose
.connect(MONGODB_CONNECTION_STRING, {
appname: 'test-db',
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(async () => {
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Use it like this
const cursor = await User.find({}).cursorBatched({ batchSize: 5 });
for await (const batch of cursor) {
// Here you can run your process with the data...
// Example:
console.log('batch', batch);
await timeout(5000);
}
});