Merge plugin for mongoose documents
Last updated 6 years ago by eherve .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mongoose-merge-plugin 
SYNC missed versions from official npm registry.


Mongoose plugin for document merging

Install npm install mongoose-merge-plugin


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


var mongoose = require('mongoose');



The method merge is added to all mongoose documents. It takes a source object as parameter and merge it in the target existing documents. It checks for existing values in the source object regarding the field path of the destination document. It does not merge the _id and __v default fields and check for the schema field option mergeable. If the option is false, the merge skip the field as well.

The method merge accept a second parameter that is the merge options. this parameter allow to specify a filter on fields when calling the merge.

  • options.fields

It is a string with field names, separeted by one or more spaces. If the field path is present in this string, the merge check if the mongoose field is mergeable and if yes it allows the merge. If before the field path a + flag is added, then the field will be merged (overriding the schema field option). If the flag is '-' the field will not be merged as well as if the field name is not present.

  • options.virtuals

It is a boolean value that activates the merging of the virtuals fields


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 ...}



Current Tags

  • 0.0.5                                ...           latest (6 years ago)

5 Versions

  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 6
Dependencies (0)
Dev Dependencies (0)

Copyright 2014 - 2017 © |