From c371500730310ad3a0362c91f16da2e1f91de8a8 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 25 Sep 2019 11:11:16 -0700 Subject: [PATCH] fix(update): cast right hand side of `$pull` as a query instead of an update for document arrays Fix #8166 --- lib/helpers/query/castUpdate.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/helpers/query/castUpdate.js b/lib/helpers/query/castUpdate.js index 8b289af7af1..37a53b5a7e6 100644 --- a/lib/helpers/query/castUpdate.js +++ b/lib/helpers/query/castUpdate.js @@ -4,6 +4,7 @@ const CastError = require('../../error/cast'); const StrictModeError = require('../../error/strict'); const ValidationError = require('../../error/validation'); const castNumber = require('../../cast/number'); +const cast = require('../../cast'); const getEmbeddedDiscriminatorPath = require('./getEmbeddedDiscriminatorPath'); const handleImmutable = require('./handleImmutable'); const utils = require('../../utils'); @@ -139,6 +140,17 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) { key = keys[i]; val = obj[key]; + // `$pull` is special because we need to cast the RHS as a query, not as + // an update. + if (op === '$pull') { + schematype = schema._getSchema(prefix + key); + if (schematype != null && schematype.schema != null) { + obj[key] = cast(schematype.schema, obj[key], options, context); + hasKeys = true; + continue; + } + } + if (val && val.constructor.name === 'Object') { // watch for embedded doc schemas schematype = schema._getSchema(prefix + key);