Repository navigation
fix(model): don't mutate the caller's update object in bulkWrite updateMany - #16526
Merged
vkarpov15 merged 1 commit intoSep 24, 2026
Merged
Conversation
…teMany castUpdateOne() clones the update before applying timestamps, defaults and the version key (Automattic#14164), but castUpdateMany() never did: it ran setDefaultsOnInsert(), applyTimestampsToUpdate(), applyTimestampsToChildren() and decorateUpdateWithVersionKey() directly on `updateMany.update`. So `bulkWrite([{ updateMany: { filter, update: { $set: doc } } }])` wrote `updatedAt` into the caller's document, and a plain update object came back with extra keys, the same problem Automattic#14164 fixed for updateOne. Clone the update first, the same way castUpdateOne() does. Re: Automattic#14164
vkarpov15
added a commit
that referenced
this pull request
Oct 19, 2026
…e-updatemany-clone fix(model): don't mutate the caller's update object in bulkWrite updateMany (backport #16526 to 8.x)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Model.bulkWrite()with anupdateManyop mutates the update object the caller passed in.castUpdateOne()clones the update before applying timestamps, defaults and the version key (added in #14164, "deep clone bulkWrite() updateOne arguments to avoid mutating documents in update"), butcastUpdateMany()never received the same treatment: it runssetDefaultsOnInsert(),applyTimestampsToUpdate(),applyTimestampsToChildren()anddecorateUpdateWithVersionKey()directly onupdateMany.update.With
updateOneinstead ofupdateMany,updatecomes back unchanged. The gh-14164 scenario (update: { $set: doc }) likewise writesupdatedAtinto the caller's document forupdateMany.This PR clones the update in
castUpdateMany()exactly the waycastUpdateOne()does, and passes the clone to every helper; the cast result is still assigned back toupdateMany.update.Examples
Added
should not modify the update object passed to updateMany (gh-14164)intest/model.test.jsnext to the existing gh-14164 test. It covers both the$set: docshape and a plain update object, and checks the write itself still lands. It fails onmasterand passes with this change.