Skip to content

Commit

Permalink
fix(update): support updating array embedded discriminator props if d…
Browse files Browse the repository at this point in the history
…iscriminator key in $elemMatch

Fix #8063
  • Loading branch information
vkarpov15 committed Oct 5, 2019
1 parent f277d94 commit 523c181
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/helpers/query/getEmbeddedDiscriminatorPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@ module.exports = function getEmbeddedDiscriminatorPath(schema, update, filter, p
if (schematype == null) {
continue;
}

type = schema.pathType(subpath);
if ((schematype.$isSingleNested || schematype.$isMongooseDocumentArrayElement) &&
schematype.schema.discriminators != null) {
const discriminators = schematype.schema.discriminators;
const discriminatorValuePath = subpath + '.' +
get(schematype, 'schema.options.discriminatorKey');
const key = get(schematype, 'schema.options.discriminatorKey');
const discriminatorValuePath = subpath + '.' + key;
const discriminatorFilterPath =
discriminatorValuePath.replace(/\.\d+\./, '.');
let discriminatorKey = null;

if (discriminatorValuePath in filter) {
discriminatorKey = filter[discriminatorValuePath];
}
if (discriminatorFilterPath in filter) {
discriminatorKey = filter[discriminatorFilterPath];
}
const wrapperPath = subpath.replace(/\.\d+$/, '');
if (schematype.$isMongooseDocumentArrayElement &&
get(filter[wrapperPath], '$elemMatch.' + key) != null) {
discriminatorKey = filter[wrapperPath].$elemMatch[key];
}

if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
continue;
}
Expand Down

0 comments on commit 523c181

Please sign in to comment.