diff --git a/docs/search.js b/docs/search.js index 83615f0ca96..a82d79669a2 100644 --- a/docs/search.js +++ b/docs/search.js @@ -122,13 +122,13 @@ async function run() { await Content.deleteMany({ version }); for (const content of contents) { if (version !== '6.x') { - content.url = `/docs/${version}/docs/${content.url}`; + content.url = `/docs/${version}/docs${content.url}`; } await content.save(); } const results = await Content. - find({ $text: { $search: 'validate' } }, { score: { $meta: 'textScore' } }). + find({ $text: { $search: 'validate' }, version }, { score: { $meta: 'textScore' } }). sort({ score: { $meta: 'textScore' } }). limit(10); diff --git a/lib/helpers/query/castUpdate.js b/lib/helpers/query/castUpdate.js index 50d56509610..2e1dde1a6c0 100644 --- a/lib/helpers/query/castUpdate.js +++ b/lib/helpers/query/castUpdate.js @@ -85,9 +85,11 @@ module.exports = function castUpdate(schema, obj, options, context, filter) { val = ret[op]; hasDollarKey = hasDollarKey || op.startsWith('$'); const toUnset = {}; - for (const key of Object.keys(val)) { - if (val[key] === undefined) { - toUnset[key] = 1; + if (val != null) { + for (const key of Object.keys(val)) { + if (val[key] === undefined) { + toUnset[key] = 1; + } } } diff --git a/test/query.test.js b/test/query.test.js index f291c5d226f..a02a37896d0 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -4314,7 +4314,7 @@ describe('Query', function() { assert.strictEqual(found[0].title, 'burrito bowl'); }); - it('update operation should remove fields set to undefined (gh-12794)', async() => { + it('update operation should remove fields set to undefined (gh-12794) (gh-12821)', async function() { const m = new mongoose.Mongoose(); await m.connect(start.uri); @@ -4338,5 +4338,15 @@ describe('Query', function() { ).lean(); assert.ok('title' in updatedDoc === false); + + const replacedDoc = await Test.findOneAndReplace( + { + _id: doc._id + }, + { title: undefined }, + { returnOriginal: false } + ).lean(); + + assert.ok('title' in replacedDoc === false); }); }); diff --git a/test/schema.test.js b/test/schema.test.js index 67e678fa184..dec8be82d56 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -2833,7 +2833,7 @@ describe('schema', function() { assert.equal(entry instanceof mongoose.Document, false); }); - it('disallows setting special properties with `add()` or constructor (gh-12085)', async function() { + it('disallows setting special properties with `add()` or constructor (gh-12085)', function() { const maliciousPayload = '{"__proto__.toString": "Number"}'; assert.throws(() => {