From d452a60a90b6ef3576ed9d58c1907da6daa3b17f Mon Sep 17 00:00:00 2001 From: Mohamed Yousef Date: Tue, 29 Aug 2023 18:46:01 +0300 Subject: [PATCH 1/3] set default value for _update when no update object is provided and versionKey is set to false --- lib/query.js | 1 + test/model.findOneAndUpdate.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/query.js b/lib/query.js index 44823cd0573..7b2a6e077fa 100644 --- a/lib/query.js +++ b/lib/query.js @@ -3810,6 +3810,7 @@ function _completeManyLean(schema, docs, path, opts) { Query.prototype._mergeUpdate = function(doc) { if (doc == null || (typeof doc === 'object' && Object.keys(doc).length === 0)) { + this._update = {}; return; } diff --git a/test/model.findOneAndUpdate.test.js b/test/model.findOneAndUpdate.test.js index 18f9e5e8692..4453420aef7 100644 --- a/test/model.findOneAndUpdate.test.js +++ b/test/model.findOneAndUpdate.test.js @@ -2184,4 +2184,20 @@ describe('model: findOneAndUpdate:', function() { /Cannot set `rawResult` option when `includeResultMetadata` is false/ ); }); + + it('successfully runs findOneAndUpdate with no update and versionKey set to false (gh-13783)', async function() { + const exampleSchema = new mongoose.Schema({ + name: String + }, { versionKey: false }); + + const ExampleModel = db.model('Example', exampleSchema); + + const document = await ExampleModel.findOneAndUpdate( + { name: 'test' }, + {}, + { upsert: true, returnDocument: 'after', returnOriginal: false } + ); + assert.ok(document); + assert.ok(document.name, 'test'); + }); }); From 7a8a160c36c605d59feb1c3d737cbe643f3a5115 Mon Sep 17 00:00:00 2001 From: Mohamed Oraby <37554568+MohOraby@users.noreply.github.com> Date: Wed, 30 Aug 2023 19:26:11 +0300 Subject: [PATCH 2/3] Fix assertion Co-authored-by: Valeriu-Andrei Florescu <49231993+valeriuflorescu16@users.noreply.github.com> --- test/model.findOneAndUpdate.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.findOneAndUpdate.test.js b/test/model.findOneAndUpdate.test.js index 4453420aef7..eee320aa968 100644 --- a/test/model.findOneAndUpdate.test.js +++ b/test/model.findOneAndUpdate.test.js @@ -2198,6 +2198,6 @@ describe('model: findOneAndUpdate:', function() { { upsert: true, returnDocument: 'after', returnOriginal: false } ); assert.ok(document); - assert.ok(document.name, 'test'); + assert.equal(document.name, 'test'); }); }); From 4b0b50c5c028aeca063366cae1b715290b6987f9 Mon Sep 17 00:00:00 2001 From: Mohamed Yousef Date: Wed, 30 Aug 2023 19:26:45 +0300 Subject: [PATCH 3/3] refactor --- lib/query.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/query.js b/lib/query.js index 7b2a6e077fa..f305b4184b0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -3809,14 +3809,14 @@ function _completeManyLean(schema, docs, path, opts) { */ Query.prototype._mergeUpdate = function(doc) { + if (!this._update) { + this._update = Array.isArray(doc) ? [] : {}; + } + if (doc == null || (typeof doc === 'object' && Object.keys(doc).length === 0)) { - this._update = {}; return; } - if (!this._update) { - this._update = Array.isArray(doc) ? [] : {}; - } if (doc instanceof Query) { if (Array.isArray(this._update)) { throw new Error('Cannot mix array and object updates');