Skip to content

Commit ca36b12

Browse files
authored
Merge pull request #15782 from jhaayushkumar/fix-bulkwrite-immutable-15781
fix(bulkWrite): pass overwriteImmutable option to castUpdate fixes #15781
2 parents b33c55d + 3c7ff93 commit ca36b12

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
@@ -2680,6 +2680,36 @@ describe('model: updateOne: ', function() {
26802680
assert.equal(doc.age, 20);
26812681
});
26822682

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

0 commit comments

Comments
 (0)