Skip to content

fleg/mongo-each

Repository files navigation

mongo-each

Build Status Coverage Status NPM Version

Queued asynchronous iterating over mongodb cursor

API

each(cursor, iteratee, [options])

  • cursor - a mongodb cursor
  • iteratee - function(doc) - iteratee function, should return promise
  • options - optional
    • concurrency - how many iteratee 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

Example

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();

License

MIT

About

Queued asynchronous iterating over mongodb cursor

Resources

License

Stars

Watchers

Forks

Packages

No packages published