An open API service indexing awesome lists of open source software.

https://github.com/jlyonsmith/mongoose-doc-merge

A Mongoose plugin that adds a simple merge method to all documents
https://github.com/jlyonsmith/mongoose-doc-merge

Last synced: 13 days ago
JSON representation

A Mongoose plugin that adds a simple merge method to all documents

Awesome Lists containing this project

README

          

# mongoose-doc-merge

This is a Mongoose plugin that adds a `merge` method to all your documents.

## Setup

Add the module:

```
npm install mongoose-doc-merge
```

Require it:

```
import mergePlugin from 'mongoose-doc-merge'
```

Initialize it:

```
mongoose.plugin(mergePlugin)
```

Use it:

```
let doc = new Model({...})
let otherDoc = new Model(req.body)

doc.merge(otherDoc)
```

The `merge()` method overwrites any path values in `doc` that exist in `otherDoc`. Path values that that have a value of `undefined` are ignored, but path values that are `null`are merged.

The algorithm doesn't do anything fancy with arrays. The new array overwrites the old one entirely.

# Why?

If you are updating documents from a partial update, for example as part of a `PUT` method in a REST API, you will want to merge new data in with existing data. It's got to be Mongoose document aware because a straight Javascript object merge doesn't work. You also don't need anything fancy. You just want the new path values to overwrite the old ones, leaving the rest of the document intact. That's what this method does.