Skip to content

Commit

Permalink
Fixes error on discriminator models if unique set on the base model (
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 authored and viveleroi committed Apr 17, 2019
1 parent da52b0c commit 7f41cd5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ module.exports = function(schema, options) {
} else if (isFunc(this.model)) {
model = this.model(this.constructor.modelName);
}
// Is this model a discriminator and the unique index is on the whole collection,
// not just the instances of the discriminator? If so, use the base model to query.
// https://github.com/Automattic/mongoose/issues/4965
if (model.baseModelName && indexOptions.partialFilterExpression == null) {
model = model.db.model(model.baseModelName);
}

model.where({ $and: conditions }).countDocuments((err, count) => {
resolve(count === 0);
Expand Down

0 comments on commit 7f41cd5

Please sign in to comment.