Skip to content

Conversation

@jhaayushkumar
Copy link

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
Author

@vkarpov15 please review and suggest changes if requires

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

1 participant