From f50d88dbc257730bc7546176eb084ddb9b8e36cd Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 17 Oct 2023 12:38:43 -0400 Subject: [PATCH] refactor: avoid relying on overwrite option for timestamps and casting re: #13578 --- lib/helpers/timestamps/setupTimestamps.js | 2 +- lib/helpers/update/applyTimestampsToUpdate.js | 5 ++--- lib/query.js | 10 +--------- test/query.test.js | 11 ----------- 4 files changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/helpers/timestamps/setupTimestamps.js b/lib/helpers/timestamps/setupTimestamps.js index 78293deb904..f6ba12b98b6 100644 --- a/lib/helpers/timestamps/setupTimestamps.js +++ b/lib/helpers/timestamps/setupTimestamps.js @@ -102,7 +102,7 @@ module.exports = function setupTimestamps(schema, timestamps) { updatedAt, this.getUpdate(), this._mongooseOptions, - this.schema + replaceOps.has(this.op) ); applyTimestampsToChildren(now, this.getUpdate(), this.model.schema); next(); diff --git a/lib/helpers/update/applyTimestampsToUpdate.js b/lib/helpers/update/applyTimestampsToUpdate.js index b48febafb69..e8d3217fbb9 100644 --- a/lib/helpers/update/applyTimestampsToUpdate.js +++ b/lib/helpers/update/applyTimestampsToUpdate.js @@ -12,10 +12,9 @@ module.exports = applyTimestampsToUpdate; * ignore */ -function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) { +function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options, isReplace) { const updates = currentUpdate; let _updates = updates; - const overwrite = get(options, 'overwrite', false); const timestamps = get(options, 'timestamps', true); // Support skipping timestamps at the query level, see gh-6980 @@ -26,7 +25,7 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio const skipCreatedAt = timestamps != null && timestamps.createdAt === false; const skipUpdatedAt = timestamps != null && timestamps.updatedAt === false; - if (overwrite) { + if (isReplace) { if (currentUpdate && currentUpdate.$set) { currentUpdate = currentUpdate.$set; updates.$set = {}; diff --git a/lib/query.js b/lib/query.js index f048b3c5660..ef945402c33 100644 --- a/lib/query.js +++ b/lib/query.js @@ -21,7 +21,6 @@ const castUpdate = require('./helpers/query/castUpdate'); const clone = require('./helpers/clone'); const completeMany = require('./helpers/query/completeMany'); const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue'); -const hasDollarKeys = require('./helpers/query/hasDollarKeys'); const helpers = require('./queryHelpers'); const immediate = require('./helpers/immediate'); const internalToObjectOptions = require('./options').internalToObjectOptions; @@ -3501,7 +3500,6 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options) { options.returnOriginal = returnOriginal; } this.setOptions(options); - this.setOptions({ overwrite: true }); return this; }; @@ -3736,13 +3734,8 @@ async function _updateThunk(op) { this._applyTranslateAliases(options); this._update = clone(this._update, options); - const isOverwriting = this._mongooseOptions.overwrite && !hasDollarKeys(this._update); + const isOverwriting = op === 'replaceOne'; if (isOverwriting) { - if (op === 'updateOne' || op === 'updateMany') { - throw new MongooseError('The MongoDB server disallows ' + - 'overwriting documents using `' + op + '`. See: ' + - 'https://mongoosejs.com/docs/deprecations.html#update'); - } this._update = new this.model(this._update, null, true); } else { this._update = this._castUpdate(this._update, this._mongooseOptions.overwrite); @@ -4046,7 +4039,6 @@ Query.prototype.replaceOne = function(conditions, doc, options, callback) { callback = undefined; } - this.setOptions({ overwrite: true }); return _update(this, 'replaceOne', conditions, doc, options, callback); }; diff --git a/test/query.test.js b/test/query.test.js index 1c4c86440b2..5cba1f62c1e 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -2962,17 +2962,6 @@ describe('Query', function() { delete db.base.options.maxTimeMS; }); - it('throws error with updateOne() and overwrite (gh-7475)', function() { - const Model = db.model('Test', Schema({ name: String })); - - return Model.updateOne({}, { name: 'bar' }, { overwrite: true }).then( - () => { throw new Error('Should have failed'); }, - err => { - assert.ok(err.message.indexOf('updateOne') !== -1); - } - ); - }); - describe('merge()', function() { it('copies populate() (gh-1790)', async function() { const Car = db.model('Car', {