Permalink
Browse files

fix(document): make `save()` succeed if strict: false with a `collect…

…ion` property

Fix #7276
  • Loading branch information...
vkarpov15 committed Dec 3, 2018
1 parent 58121c6 commit b6c81977e896f288bec38b177a455a4f88fa9ecd
Showing with 9 additions and 4 deletions.
  1. +9 −4 lib/model.js
@@ -47,7 +47,8 @@ const VERSION_WHERE = 1;
const VERSION_INC = 2;
const VERSION_ALL = VERSION_WHERE | VERSION_INC;
const modelSymbol = Symbol.for('mongoose:Model');
const modelCollectionSymbol = Symbol.for('mongoose#Model#collection');
const modelSymbol = Symbol.for('mongoose#Model');
const schemaMixedSymbol = require('./schema/symbols').schemaMixedSymbol;
/**
@@ -115,6 +116,8 @@ Model.prototype.db;
/**
* Collection the model uses.
*
* This property is read-only. Modifying this property is a no-op.
*
* @api public
* @property collection
* @memberOf Model
@@ -213,7 +216,7 @@ Model.prototype.$__handleSave = function(options, callback) {
}
this.$__version(true, obj);
this.collection.insertOne(obj, saveOptions, function(err, ret) {
this[modelCollectionSymbol].insertOne(obj, saveOptions, function(err, ret) {
if (err) {
_this.isNew = true;
_this.emit('isNew', true);
@@ -258,7 +261,7 @@ Model.prototype.$__handleSave = function(options, callback) {
}
}
this.collection.updateOne(where, delta[1], saveOptions, function(err, ret) {
this[modelCollectionSymbol].updateOne(where, delta[1], saveOptions, function(err, ret) {
if (err) {
callback(err);
return;
@@ -880,7 +883,7 @@ Model.prototype.$__remove = function $__remove(options, cb) {
return cb(where);
}
this.collection.deleteOne(where, options, err => {
this[modelCollectionSymbol].deleteOne(where, options, err => {
if (!err) {
this.$__.isDeleted = true;
this.emit('remove', this);
@@ -4418,6 +4421,7 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
collectionName,
collectionOptions
);
model.prototype[modelCollectionSymbol] = model.prototype.collection;
// apply methods and statics
applyMethods(model, schema);
@@ -4510,6 +4514,7 @@ Model.__subclass = function subclass(conn, schema, collection) {
};
Model.prototype.collection = conn.collection(collection, collectionOptions);
Model.prototype[modelCollectionSymbol] = Model.prototype.collection;
Model.collection = Model.prototype.collection;
// Errors handled internally, so ignore
Model.init(() => {});

0 comments on commit b6c8197

Please sign in to comment.