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

@reis/mongoose-upsert-many

v3.0.0

Published

A plugin that adds an upsertMany bulk op to Mongoose schemas

Downloads

183

Readme

@reis/mongoose-upsert-many

npm version github issues

A plugin that adds an upsertMany bulk op to Mongoose schemas

Installation

You can install this package using yarn or npm.

#yarn
yarn add @reis/mongoose-upsert-many

#npm
npm install @reis/mongoose-upsert-many --save

Usage

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

import mongoose from 'mongoose'
import {upsertMany} from '@reis/mongoose-upsert-many'

//Global plugin
mongoose.plugin(upsertMany)

Or for a specific (sub) schema:

import mongoose from 'mongoose'
import {upsertMany} from '@reis/mongoose-upsert-many'
const {Schema} = mongoose

//Define schema
const MySchema = new Schema(/* ... */}, {

  //Schema-wide configuration for the upsertMany plugin
  upsertMany: {
    matchFields: ['field1', 'field2'],
    type: 'replaceOne',
    ensureModel: true,
  },
})

//Apply plugin
MySchema.plugin(upsertMany)

This plugin will expose a static upsertMany method on your models which you can use to perform bulk upsert operations:


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

//Optionally, overwrite schema-wide configuration
const config = {matchFields: ['foo', 'bar.nested']}

//Perform bulk operation
const result = await MyModel.upsertMany(items, config)

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

Configuration

The following configuration options are available and can be passed either via the schema options or as the last parameter in the upsertMany method:

type: The bulk op type, supports any of the available bulkWrite ops, although the insert or delete ops will obviously not perform an upsert. Default value is updateOne.

matchFields: Match fields are fields that are used to create the filter criteria for the upsert operations. For example, passing ['name'] will try to match each item on the value of its name property. Default value is ['_id'].

ensureModel: Items you pass in to the plugin can be either mongoose Models or raw data. When this flag is set to true, items are always converted to Mongoose models to ensure schema validation and any schema defaults are applied. Default value is false.

ordered: Whether the bulk operations are to be ordered or not. Passed to Mongoose. Default value is true.

toObjectConfig: Configuration to pass to the toObject method when converting Mongoose models back to plain items, safe for insertion into the bulk operation. Default value is {depopulate: true, versionKey: false}.

Issues & feature requests

Please report any bugs, issues, suggestions and feature requests in the mongoose-upsert-many issue tracker.

License

(MIT License)

Copyright 2018-2023, Adam Reis