1.0.4 • Published 3 years ago

mongoose-cursorbatched v1.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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

Example result

image