Skip to content

asotoglez/mongoose-cursorbatched

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A mongoose plugin that allows iterations over batches with cursor.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published