Skip to content

Commit 2decab7

Browse files
jhaayushkumarvkarpov15
authored andcommitted
fix(bulkWrite): pass overwriteImmutable option to castUpdate fixes #15781
1 parent 6de5132 commit 2decab7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/helpers/model/castBulkWrite.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ module.exports.castUpdateOne = function castUpdateOne(originalModel, updateOne,
118118
if (model.schema.$timestamps != null && doInitTimestamps) {
119119
const createdAt = model.schema.$timestamps.createdAt;
120120
const updatedAt = model.schema.$timestamps.updatedAt;
121-
applyTimestampsToUpdate(now, createdAt, updatedAt, update, {});
121+
applyTimestampsToUpdate(now, createdAt, updatedAt, update, {
122+
timestamps: updateOne.timestamps
123+
});
122124
}
123125

124126
if (doInitTimestamps) {
@@ -150,7 +152,8 @@ module.exports.castUpdateOne = function castUpdateOne(originalModel, updateOne,
150152
strict: strict,
151153
upsert: updateOne.upsert,
152154
arrayFilters: updateOne.arrayFilters,
153-
overwriteDiscriminatorKey: updateOne.overwriteDiscriminatorKey
155+
overwriteDiscriminatorKey: updateOne.overwriteDiscriminatorKey,
156+
overwriteImmutable: updateOne.overwriteImmutable
154157
}, model, updateOne['filter']);
155158

156159
return updateOne;
@@ -185,7 +188,9 @@ module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMan
185188
if (model.schema.$timestamps != null && doInitTimestamps) {
186189
const createdAt = model.schema.$timestamps.createdAt;
187190
const updatedAt = model.schema.$timestamps.updatedAt;
188-
applyTimestampsToUpdate(now, createdAt, updatedAt, updateMany['update'], {});
191+
applyTimestampsToUpdate(now, createdAt, updatedAt, updateMany['update'], {
192+
timestamps: updateMany.timestamps
193+
});
189194
}
190195
if (doInitTimestamps) {
191196
applyTimestampsToChildren(now, updateMany['update'], model.schema);
@@ -208,7 +213,8 @@ module.exports.castUpdateMany = function castUpdateMany(originalModel, updateMan
208213
strict: strict,
209214
upsert: updateMany.upsert,
210215
arrayFilters: updateMany.arrayFilters,
211-
overwriteDiscriminatorKey: updateMany.overwriteDiscriminatorKey
216+
overwriteDiscriminatorKey: updateMany.overwriteDiscriminatorKey,
217+
overwriteImmutable: updateMany.overwriteImmutable
212218
}, model, updateMany['filter']);
213219
};
214220

test/model.updateOne.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,36 @@ describe('model: updateOne: ', function() {
26822682
assert.equal(doc.age, 20);
26832683
});
26842684

2685+
it('overwriting immutable createdAt with bulkWrite (gh-15781)', async function() {
2686+
const start = new Date().valueOf();
2687+
const schema = Schema({
2688+
createdAt: {
2689+
type: mongoose.Schema.Types.Date,
2690+
immutable: true
2691+
},
2692+
name: String
2693+
}, { timestamps: true });
2694+
2695+
const Model = db.model('Test', schema);
2696+
2697+
await Model.create({ name: 'gh-15781' });
2698+
let doc = await Model.collection.findOne({ name: 'gh-15781' });
2699+
assert.ok(doc.createdAt.valueOf() >= start);
2700+
2701+
const createdAt = new Date('2011-06-01');
2702+
assert.ok(createdAt.valueOf() < start.valueOf());
2703+
await Model.bulkWrite([{
2704+
updateOne: {
2705+
filter: { _id: doc._id },
2706+
update: { name: 'gh-15781 update', createdAt },
2707+
overwriteImmutable: true,
2708+
timestamps: false
2709+
}
2710+
}]);
2711+
doc = await Model.collection.findOne({ name: 'gh-15781 update' });
2712+
assert.equal(doc.createdAt.valueOf(), createdAt.valueOf());
2713+
});
2714+
26852715
it('updates buffers with `runValidators` successfully (gh-8580)', async function() {
26862716
const Test = db.model('Test', Schema({
26872717
data: { type: Buffer, required: true }

0 commit comments

Comments
 (0)