From 06cca17419ddc839a5f086d2b405c94cb0ed2113 Mon Sep 17 00:00:00 2001 From: Kev Date: Sun, 13 May 2018 14:53:56 -0400 Subject: [PATCH 1/2] add fix for #6439 to 4.x --- lib/services/query/castUpdate.js | 2 +- test/query.test.js | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/services/query/castUpdate.js b/lib/services/query/castUpdate.js index fa519cc1907..c89de2cc902 100644 --- a/lib/services/query/castUpdate.js +++ b/lib/services/query/castUpdate.js @@ -193,7 +193,7 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) { (utils.isObject(val) && Object.keys(val).length === 0); } } else { - var checkPath = (key === '$each' || key === '$or' || key === '$and') ? + var checkPath = (key === '$each' || key === '$or' || key === '$and' || key === '$in') ? pref : prefix + key; schematype = schema._getSchema(checkPath); diff --git a/test/query.test.js b/test/query.test.js index f5807a6e71e..b53b6d2c3b2 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -9,6 +9,7 @@ var Schema = mongoose.Schema; var assert = require('power-assert'); var random = require('../lib/utils').random; var Query = require('../lib/query'); +var co = require('co'); /** * Test. @@ -918,6 +919,45 @@ describe('Query', function() { done(); }); + it('doesn\'t wipe out $in (gh-6439)', function () { + var embeddedSchema = new Schema({ + name: String + }, { _id: false }); + + var catSchema = new Schema({ + name: String, + props: [embeddedSchema] + }); + + var Cat = db.model('gh6439', catSchema); + var kitty = new Cat({ + name: 'Zildjian', + props: [ + { name: 'invalid' }, + { name: 'abc' }, + { name: 'def' } + ] + }); + + return co(function* () { + yield kitty.save(); + var cond = { _id: kitty._id }; + var update = { + $pull: { + props: { + $in: [ + { name: 'invalid' }, + { name: 'def' } + ] + } + } + }; + yield Cat.update(cond, update); + let found = yield Cat.findOne(cond); + assert.strictEqual(found.props[0].name, 'abc'); + }); + }); + it('subdocument array with $ne: null should not throw', function(done) { var query = new Query({}, {}, null, p1.collection); var Product = db.model('Product'); From 48f1fc30544bd3afbefb986e50f153c3c687effd Mon Sep 17 00:00:00 2001 From: Kev Date: Mon, 14 May 2018 05:54:33 -0400 Subject: [PATCH 2/2] remove co for compatibility --- test/query.test.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/test/query.test.js b/test/query.test.js index b53b6d2c3b2..7e273b24a15 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -9,7 +9,6 @@ var Schema = mongoose.Schema; var assert = require('power-assert'); var random = require('../lib/utils').random; var Query = require('../lib/query'); -var co = require('co'); /** * Test. @@ -919,7 +918,7 @@ describe('Query', function() { done(); }); - it('doesn\'t wipe out $in (gh-6439)', function () { + it('doesn\'t wipe out $in (gh-6439)', function(done) { var embeddedSchema = new Schema({ name: String }, { _id: false }); @@ -939,8 +938,8 @@ describe('Query', function() { ] }); - return co(function* () { - yield kitty.save(); + kitty.save(function(err) { + assert.ifError(err); var cond = { _id: kitty._id }; var update = { $pull: { @@ -952,9 +951,14 @@ describe('Query', function() { } } }; - yield Cat.update(cond, update); - let found = yield Cat.findOne(cond); - assert.strictEqual(found.props[0].name, 'abc'); + Cat.update(cond, update, function(err) { + assert.ifError(err); + Cat.findOne(cond, function(err, found) { + assert.ifError(err); + assert.strictEqual(found.props[0].name, 'abc'); + done(); + }); + }); }); });