mongoose-bulkop

1.0.1 • Public • Published

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)

Package Sidebar

Install

npm i mongoose-bulkop

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

12 kB

Total Files

10

Last publish

Collaborators

  • timweri