Have ideas to improve npm?Join in the discussion! »

    mongo-each

    2.0.1 • Public • Published

    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

    Install

    npm i mongo-each

    DownloadsWeekly Downloads

    14

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    4.31 kB

    Total Files

    4

    Last publish

    Collaborators

    • avatar