From c1e8db6df047422819c21b0be8a2fd9540e36313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mu=C3=B1oz?= Date: Wed, 17 Apr 2019 19:20:52 -0400 Subject: [PATCH] fix conditions that changes "conditions" variable. early return for check _id in old document. simplify model detection. --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 17160c8..97ad93f 100644 --- a/index.js +++ b/index.js @@ -84,8 +84,12 @@ module.exports = function(schema, options) { each(this._conditions, (value, key) => { conditions[key] = { $ne: value }; }); - } else if (this._id) { + } else if (pathName !== '_id') { + // if it's not new then it always has _id conditions._id = { $ne: this._id }; + } else { + // if is not new and is not query and the pathName is _id then is the same document no need to check anything + return resolve(true); } } @@ -97,8 +101,9 @@ module.exports = function(schema, options) { model = this.model; } else if (isSubdocument) { model = this.ownerDocument().model(this.ownerDocument().constructor.modelName); - } else if (isFunc(this.model)) { - model = this.model(this.constructor.modelName); + } else if (this.constructor.modelName) { + // if the constructor has modelName then the constructor is the model + model = this.constructor; } // Is this model a discriminator and the unique index is on the whole collection,