Giter Site home page Giter Site logo

mongoose-merge-plugin's Introduction

mongoose-merge-plugin

Mongoose plugin for document merging

Install

npm install mongoose-merge-plugin

Loading

var merge = require('mongoose-merge-plugin');

Initialize

var mongoose = require('mongoose');
  
mongoose.plugin(merge);

Usage

The merge method is added to all mongoose documents. It takes a source object as the first parameter and merges it into the target documents. It compares the source object and destination document for paths that match. It does not merge the _id and __v default fields. It checks for the schema field option mergeable; If the option is false, merge skips the field.

The merge method accepts a second parameter: merge options. This parameter allows you to specify a filter that will be applied when merging.

  • options.fields

A string of paths separated by one or more spaces. If a path is present in this string, the merge checks whether the Mongoose field is mergeable. If so, it allows the merge. Add a + or - flag before a path to override the schema option and force a field to merge or not merge, respectively.

  • options.virtuals

If true, virtual fields will also be merged.

Example

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
    
var schema = new Schema({
  name: String,
  description: String,
  notMergedField: { type: String, mergeable: false }
});

var Test = mongoose.model('Test', schema);

var test = new Test({ name: "test", description: "desc", notMergedField: "testNMF" });
console.log(test); // LOG: { i_id: ..., name: test, description: desc, notMergedField: testNMF ...}

test.merge({ name: "testChanged", description: "descChanged", notMergedField: "testNMFChanged" });
console.log(test); // LOG: { i_id: ..., name: testChanged, description: descChanged, notMergedField: testNMF ...}

test.merge({ name: "test", description: "desc" }, { fields: "-description" });
console.log(test); // LOG: { i_id: ..., name: test, description: descChanged, notMergedField: testNMF ...}

Support

it-tweaks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.