Repository navigation
fix(array): keep earlier pushed values when push() is called again with $each - #16619
Merged
vkarpov15 merged 1 commit intoNov 9, 2026
Conversation
…th $each
A second push() in the { $each } form replaced the pending $push, so save()
only wrote the last batch. Add its values to the pending $each instead, and
save the whole array with $set when either push has $position, $slice or
$sort, as push() already does for two different $position values (Automatticgh-14244).
A plain push() after a positioned one falls back to $set too.
Fix Automattic#16618
vkarpov15
approved these changes
Nov 9, 2026
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
Fixes #16618.
push(3)thenpush({ $each: [4] })before a save sends only{ $push: { nums: { $each: [4] } } }, so the 3 never reaches the database._registerAtomic()replaces a pending$pushwhenever the new one has$each, andpush()only falls back to$setfor two different$positionvalues (gh-14244). Same with two{ $each }pushes. The other order breaks too: a plainpush(3)afterpush({ $each: [0], $position: 0 })joins the positioned$eachand is saved at the front.Now a second
push()adds its values to the pending$eachinstead of replacing it. If either push has$position,$sliceor$sort, the whole array is saved with$set, like the different$positioncase already is.One catch: if a
$pushis already pending, a$sliceor$sorton either push isn't sent, because the array is saved as the document holds it. A single push still sends them. I didn't touch a plainpush()after a pending$pushwith$sliceor$sort, it still joins that$pushlike on master.Examples
npx mocha test/types.array.test.js -g "gh-16618". The 3 new tests (one on a document array) fail on master and pass here.test/document.test.jsstill passes, different behavior of arrays in 6.x when compared to 5.x #14244 included.npm run test:ci, 4677 passing, 47 pending, 0 failing.npx eslinton the changed files is clean.Node 22.23.3, MongoDB 8.2.6 (mongodb-memory-server), Linux.