Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4b1aaac

Browse files
authored
Merge pull request #10050 from SoftwareSing/fix-bulkwrite-with-timestamps-false
fix: make bulkWrite can work with `timestamps: false`
2 parents 5ffbb8e + 3759f34 commit 4b1aaac

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/helpers/model/castBulkWrite.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
2020
const model = decideModelByObject(originalModel, op['insertOne']['document']);
2121

2222
const doc = new model(op['insertOne']['document']);
23-
if (model.schema.options.timestamps != null) {
23+
if (model.schema.options.timestamps) {
2424
doc.initializeTimestamps();
2525
}
2626
if (options.session != null) {
@@ -147,7 +147,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
147147

148148
// set `skipId`, otherwise we get "_id field cannot be changed"
149149
const doc = new model(op['replaceOne']['replacement'], strict, true);
150-
if (model.schema.options.timestamps != null) {
150+
if (model.schema.options.timestamps) {
151151
doc.initializeTimestamps();
152152
}
153153
if (options.session != null) {
@@ -221,4 +221,4 @@ function decideModelByObject(model, object) {
221221
model = getDiscriminatorByValue(model, object[discriminatorKey]) || model;
222222
}
223223
return model;
224-
}
224+
}

test/model.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5780,6 +5780,35 @@ describe('Model', function() {
57805780
assert.equal(people[3].age, 30);
57815781
});
57825782
});
5783+
5784+
it('insertOne and replaceOne should not throw an error when set `timestamps: false` in schmea (gh-10048)', function() {
5785+
const schema = new Schema({ name: String }, { timestamps: false });
5786+
const Model = db.model('Test', schema);
5787+
5788+
return co(function*() {
5789+
yield Model.create({ name: 'test' });
5790+
5791+
yield Model.bulkWrite([
5792+
{
5793+
insertOne: {
5794+
document: { name: 'insertOne-test' }
5795+
}
5796+
},
5797+
{
5798+
replaceOne: {
5799+
filter: { name: 'test' },
5800+
replacement: { name: 'replaceOne-test' }
5801+
}
5802+
}
5803+
]);
5804+
5805+
for (const name of ['insertOne-test', 'replaceOne-test']) {
5806+
const doc = yield Model.findOne({ name });
5807+
assert.strictEqual(doc.createdAt, undefined);
5808+
assert.strictEqual(doc.updatedAt, undefined);
5809+
}
5810+
});
5811+
});
57835812
});
57845813

57855814
it('insertMany with Decimal (gh-5190)', function(done) {

0 commit comments

Comments
 (0)