Queued asynchronous iterating over mongodb cursor
cursor
- a mongodb cursoriteratee
-function(doc)
- iteratee function, should return promiseoptions
- optionalconcurrency
- how manyiteratee
functions should be run in parallel (default:100
)batch
- batch mode (default:false
)batchSize
- batch size (default:10
)
Returns promise which is resolves when all iteratee functions have finished, or rejects if an error occurs
const {MongoClient} = require('mongodb');
const each = require('mongo-each');
async function main() {
const db = await MongoClient.connect('mongodb://127.0.0.1:27017/mongo-each');
const collection = db.collection('data');
const cursor = collection.find();
await each(collection.find(), async (doc) => {
console.log(doc)
await delay(100);
}, {concurrency: 50});
}
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
main();
MIT