npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-batches

v0.0.2

Published

A Mongoose plugin to work with large amounts of data via async batch processing.

Downloads

251

Readme

Mongoose Find In Batches

Docs are written in ES5, feel free to implement in ES6 etc...

setup: myModel.js

var findInBatches = require('mongoose-batches');

var myModel = new Schema({
  //... Your Schema Here
});

myModel.plugin(findInBatches);

//... etc

usage: someFile.js

var query = {foo : true};
var options = {batchSize : 1000};

myModel.findInBatches(query, options, function (err, docs, next) {
 //.. Do stuff
 next();
}).then(function () {
  // All done
});

About the parameters

  • 1st param is the query to send to myModel.Find

  • 2nd param is the options, currenty only batchSize, select, and queryGenerator.

    • queryGenerator is a function used to modify the query in addition to the query parameter that is passed to myModel.Find. It allows you to use the predefined queries build onto the model.
  • 3rd param is the "Batch Handler". A function that receives each batch of documents.

    • The batch handler's parameters are
      • 1 any errors returned from the find query
      • 2 (Array) the batch of documents
      • 3 the function to invoke the next batch. (explicitly passing in 'cancel' to nextBatch will stop the operation and invoke the final promise. nextBatch('cancel'))
      • 4 The count of docs that matched the query
      • 5 The amount of docs left to find (actual documents unprocessed by your batchHandler can be found by (documentsRemaining + docs.length) )

Options

var options = {
    batchSize : 1000,
    select : {
        name : 1,
        email : 1,
        phone : 1
    },
    queryGenerator: function(query) {
      return query.where({ name: {$ne: null}})
    }
};

The select option is an object that gets passed directly to mongoose's select function. The above example would grab 1000 document batches with only the name, email, phone, and _id keys. (_id is always included)

Final resolution

This is done via promise then. The promise will get resolved after all documents have been processed, or the cancel command is sent into the nextBatch function.