From 9559c4654fb5f826838a07b9093bb44785d2c06e Mon Sep 17 00:00:00 2001 From: SoftwareSing Date: Sat, 20 Mar 2021 21:16:29 +0800 Subject: [PATCH 1/4] test(bulkwrite): repro #10048 --- test/model.test.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/model.test.js b/test/model.test.js index 818a23a79dc..41185f2151c 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -5780,6 +5780,35 @@ describe('Model', function() { assert.equal(people[3].age, 30); }); }); + + it('insertOne and replaceOne should not throw an error when set `timestamps: false` in schmea', function() { + const schema = new Schema({ name: String }, { timestamps: false }); + const Model = db.model('Test', schema); + + return co(function*() { + yield Model.create({ name: 'test' }); + + yield Model.bulkWrite([ + { + insertOne: { + document: { name: 'insertOne-test' } + } + }, + { + replaceOne: { + filter: { name: 'test' }, + replacement: { name: 'replaceOne-test' } + } + } + ]); + + for (const name of ['insertOne-test', 'replaceOne-test']) { + const doc = yield Model.findOne({ name }); + assert.strictEqual(doc.createdAt, undefined); + assert.strictEqual(doc.updatedAt, undefined); + } + }); + }); }); it('insertMany with Decimal (gh-5190)', function(done) { From 0101ab8e07f32ae3f98f01e2c4cec12cc6874234 Mon Sep 17 00:00:00 2001 From: SoftwareSing Date: Sat, 20 Mar 2021 21:20:21 +0800 Subject: [PATCH 2/4] fix(bulkwrite): make bulkWrite can work with `timestamps: false` fixes #10048 --- lib/helpers/model/castBulkWrite.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers/model/castBulkWrite.js b/lib/helpers/model/castBulkWrite.js index 6e7a8300754..d3aaea99f08 100644 --- a/lib/helpers/model/castBulkWrite.js +++ b/lib/helpers/model/castBulkWrite.js @@ -20,7 +20,7 @@ module.exports = function castBulkWrite(originalModel, op, options) { const model = decideModelByObject(originalModel, op['insertOne']['document']); const doc = new model(op['insertOne']['document']); - if (model.schema.options.timestamps != null) { + if (doc.initializeTimestamps) { doc.initializeTimestamps(); } if (options.session != null) { @@ -147,7 +147,7 @@ module.exports = function castBulkWrite(originalModel, op, options) { // set `skipId`, otherwise we get "_id field cannot be changed" const doc = new model(op['replaceOne']['replacement'], strict, true); - if (model.schema.options.timestamps != null) { + if (doc.initializeTimestamps) { doc.initializeTimestamps(); } if (options.session != null) { From 003e4777130194a8cfdb35e53c0deedd53690b7e Mon Sep 17 00:00:00 2001 From: SoftwareSing Date: Sat, 20 Mar 2021 21:27:41 +0800 Subject: [PATCH 3/4] add missing issue number --- test/model.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.test.js b/test/model.test.js index 41185f2151c..596f66d91e6 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -5781,7 +5781,7 @@ describe('Model', function() { }); }); - it('insertOne and replaceOne should not throw an error when set `timestamps: false` in schmea', function() { + it('insertOne and replaceOne should not throw an error when set `timestamps: false` in schmea (gh-10048)', function() { const schema = new Schema({ name: String }, { timestamps: false }); const Model = db.model('Test', schema); From 3759f3405b986b877461f9ef0b978b193fba759b Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 22 Mar 2021 16:42:40 -0400 Subject: [PATCH 4/4] chore: address CR comments --- lib/helpers/model/castBulkWrite.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/helpers/model/castBulkWrite.js b/lib/helpers/model/castBulkWrite.js index d3aaea99f08..d3f0e9a2e1d 100644 --- a/lib/helpers/model/castBulkWrite.js +++ b/lib/helpers/model/castBulkWrite.js @@ -20,7 +20,7 @@ module.exports = function castBulkWrite(originalModel, op, options) { const model = decideModelByObject(originalModel, op['insertOne']['document']); const doc = new model(op['insertOne']['document']); - if (doc.initializeTimestamps) { + if (model.schema.options.timestamps) { doc.initializeTimestamps(); } if (options.session != null) { @@ -147,7 +147,7 @@ module.exports = function castBulkWrite(originalModel, op, options) { // set `skipId`, otherwise we get "_id field cannot be changed" const doc = new model(op['replaceOne']['replacement'], strict, true); - if (doc.initializeTimestamps) { + if (model.schema.options.timestamps) { doc.initializeTimestamps(); } if (options.session != null) { @@ -221,4 +221,4 @@ function decideModelByObject(model, object) { model = getDiscriminatorByValue(model, object[discriminatorKey]) || model; } return model; -} \ No newline at end of file +}