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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-bulkop

v1.0.1

Published

A plugin that adds bulk operations to Mongoose schemas

Downloads

8

Readme

mongoose-bulkop

npm version node dependencies github issues

A plugin that adds bulk operations to Mongoose schemas.

Features

This plugin will expose multiple static bulk operation methods on your models which you can use to perform bulk operations while still making use of Mongoose's middlewares and validatiors.

The supported bulk operations are:

  • Bulk.find.remove
  • Bulk.find.removeOne
  • Bulk.find.replaceOne and Bulk.find.upsert.replaceOne
  • Bulk.find.updateOne and Bulk.find.upsert.updateOne
  • Bulk.find.update and Bulk.find.upsert.update
  • Bulk.insert

Details about these operations can be found at https://docs.mongodb.com/manual/reference/method/js-bulk/.

Installation

Usage

Setup as a global plugin for all Mongoose schema's:

const mongoose = require('mongoose');
const bulkOp = require('mongoose-bulkop');

mongoose.plugin(bulkOp);

Or for a specific (sub) schema:

const mongoose = require('mongoose');
const bulkOp = require('mongoose-bulkop');
const {Schema} = mongoose;

const MySchema = new Schema({});
MySchema.plugin(bulkOp);

This plugin will expose the following static methods on your models:

  • bulkUpsertReplaceOne(items, matchFields)
  • bulkUpsertUpdateOne(items, matchFields)
  • bulkUpsertUpdate(items, matchFields)
  • bulkReplaceOne(items, matchFields)
  • bulkUpdateOne(items, matchFields)
  • bulkUpdate(items, matchFields)
  • bulkRemoveOne(items, matchFields)
  • bulkRemove(items, matchFields)
  • bulkInsert(items)

where

  • items is the array of items you would want to remove from, update, replace or insert into the collection.
  • matchFields is an array of field names that will be processed and passed to Bulk.find to identify which items to execute the remove, update or replace operation on.

The return value of these methods is an BulkWriteResult() object that contains information about the bulk operations:

BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 2,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

An example of usage:


//Large amount of items
const items = [
  ...
];

//Fields to match on for upsert condition
const matchFields = ['foo', 'bar.nested'];

//Perform bulk operation
const result = await MyModel.bulkUpsertReplaceOne(items, matchFields);

//Returns promise with MongoDB bulk result object
console.log(result.nUpserted + result.nModified, 'items processed');

There is a way to avoid manually declaring the match fields before an operation. We can set in the schema the fields that, by default, will be used as match fields:

const MySchema = new Schema({
  // fields of schema
}, {
  defaultBulkMatchFields: ['field1', 'field2']
});

Credits

License

(MIT License)