Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default value for _update when no update object is provided and versionKey is set to false #13795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3809,13 +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)) {
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');
Expand Down
16 changes: 16 additions & 0 deletions test/model.findOneAndUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.equal(document.name, 'test');
});
});