Skip to content

Commit

Permalink
Merge pull request #10050 from SoftwareSing/fix-bulkwrite-with-timest…
Browse files Browse the repository at this point in the history
…amps-false

fix: make bulkWrite can work with `timestamps: false`
  • Loading branch information
vkarpov15 committed Mar 22, 2021
2 parents 5ffbb8e + 3759f34 commit 4b1aaac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/helpers/model/castBulkWrite.js
Expand Up @@ -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 (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
Expand Down Expand Up @@ -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 (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
Expand Down Expand Up @@ -221,4 +221,4 @@ function decideModelByObject(model, object) {
model = getDiscriminatorByValue(model, object[discriminatorKey]) || model;
}
return model;
}
}
29 changes: 29 additions & 0 deletions test/model.test.js
Expand Up @@ -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 (gh-10048)', 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) {
Expand Down

0 comments on commit 4b1aaac

Please sign in to comment.