mongoose-cursorbatched
v1.0.4
Published
A mongoose plugin that allows iterations over batches with cursor.
Downloads
191
Maintainers
Readme
mongoose-cursorbatched
A mongoose plugin that allows iterations over batches with cursor.
Installation
$ npm i --save mongoose-cursorbatched
Parameters
batchSize: Number of records per batch (default: 20)
How to use
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);
}
});