Skip to content

Conversation

@jhaayushkumar
Copy link
Contributor

Fixes #15781

Summary

When using bulkWrite with updateOne or updateMany operations, the overwriteImmutable option was not being passed through to castUpdate. This prevented users from updating immutable timestamp fields like createdAt even when explicitly setting overwriteImmutable: true and timestamps: false.

The issue was that in castBulkWrite.js, the overwriteImmutable option from the operation was not included in the options object passed to castUpdate, causing handleImmutable to remove the immutable field from the update.

This PR fixes the issue by:

  1. Passing overwriteImmutable option from the operation to castUpdate in both castUpdateOne and castUpdateMany
  2. Passing timestamps option to applyTimestampsToUpdate to prevent deletion of createdAt when timestamps: false is set

Examples

const personSchema = new mongoose.Schema(
  { name: { type: String, required: true } },
  { timestamps: true }
);
const PersonModel = mongoose.model('Person', personSchema);

const person = await PersonModel.create({ name: 'John' });

// Before this fix: createdAt would NOT be updated
// After this fix: createdAt IS updated to new Date(0)
await PersonModel.bulkWrite([{
  updateOne: {
    filter: { name: 'John' },
    update: { createdAt: new Date(0) },
    timestamps: false,
    overwriteImmutable: true
  }
}]);

@jhaayushkumar
Copy link
Contributor Author

@vkarpov15 please review and suggest changes if requires

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where the overwriteImmutable option was not being respected in bulkWrite operations with updateOne or updateMany. The fix ensures that users can update immutable timestamp fields like createdAt when explicitly setting both overwriteImmutable: true and timestamps: false.

Key changes:

  • Pass overwriteImmutable option from operation to castUpdate in both castUpdateOne and castUpdateMany
  • Pass timestamps option to applyTimestampsToUpdate to prevent deletion of createdAt when timestamps: false is set
  • Add comprehensive test coverage for the fix

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/helpers/model/castBulkWrite.js Added overwriteImmutable to options passed to castUpdate and timestamps to options passed to applyTimestampsToUpdate in both castUpdateOne and castUpdateMany functions
test/model.updateOne.test.js Added test case verifying that immutable createdAt can be overwritten using bulkWrite with overwriteImmutable: true and timestamps: false

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍 will backport to 7.x and 8.x.

@vkarpov15 vkarpov15 merged commit ca36b12 into Automattic:master Nov 25, 2025
38 checks passed
@vkarpov15 vkarpov15 added this to the 9.0.1 milestone Nov 25, 2025
vkarpov15 added a commit that referenced this pull request Nov 25, 2025
@gaastonsr
Copy link

Ty!

vkarpov15 added a commit that referenced this pull request Dec 4, 2025
fix: allow timestamps and overwriteImmutable in TS and JS for 7.x
vkarpov15 added a commit that referenced this pull request Dec 4, 2025
fix(bulkWrite): pass overwriteImmutable option to castUpdate fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timestamp createdAt can't be updated using bulkWrite

3 participants