fix(documentarray): reindex subdocs after reordering - #16282
Merged
vkarpov15 merged 18 commits intoJun 22, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes document array subdocument index bookkeeping after removals so later nested mutations mark the correct path.
Changes:
- Reindexes surviving subdocuments after
pull(),shift(), andsplice(). - Preserves populated cache updates in the same wrappers.
- Adds regression coverage for saving after removal and mutating the first remaining subdocument.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/types/documentArray/methods/index.js |
Adds subdocument index restamping after selected document-array removal operations. |
test/types.documentarray.test.js |
Adds regression tests for pull(), splice(), and shift() reindex behavior. |
AbdelrahmanHafez
force-pushed
the
fix/document-array-reindex-after-removal
branch
from
May 13, 2026 19:48
14e4e57 to
f1f0559
Compare
| */ | ||
|
|
||
| reverse() { | ||
| const ret = _baseReverse.call(this); |
vkarpov15
approved these changes
Jun 22, 2026
vkarpov15
left a comment
Collaborator
There was a problem hiding this comment.
I spotted one issue to follow up on, but otherwise LGTM this is a meaningful improvement
|
|
||
| for (let i = 0; i < rawArray.length; ++i) { | ||
| if (typeof rawArray[i]?.$setIndex === 'function') { | ||
| rawArray[i].$setIndex(i); |
Collaborator
There was a problem hiding this comment.
There's a related error with $setIndex(): $setIndex() adds new validation error keys, but retains existing ones. For example:
user.addresses[1].invalidate('city', 'invalid city');
user.addresses.sort((a, b) => a.sequence - b.sequence);
user.$__.validationError.errors; // Has both `addresses.1.city` and `addresses.0.city` presuming that the invalid city was sorted into position 0
Collaborator
|
Gonna squash this to make it easier to backport to 8.x. |
vkarpov15
pushed a commit
that referenced
this pull request
Jun 22, 2026
* test(documentarray): cover subdoc indexes after removal * fix(documentarray): reindex subdocs after removal * test(documentarray): cover subdoc indexes after atomic shift * fix(documentarray): reindex subdocs after atomic shift * test(documentarray): cover indexes after reordering * fix(documentarray): reindex subdocs after reordering * test(documentarray): cover populated paths after sort * fix(documentarray): refresh populated paths after sort * test(documentarray): cover append and reverse reordering * fix(documentarray): handle reverse and positioned push reindexing * test(documentarray): cover reverse atomics after push * fix(documentarray): register reverse as full array update * test(documentarray): move initial save into createTestContext * test(documentarray): assert exact update deltas and modified paths * test(documentarray): cover populated paths after pop * fix(documentarray): refresh populated paths after pop * docs(documentarray): normalize MDN links and fix shift() link target
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.
Document array operations that remove or reorder entries can leave surviving subdocs with stale
__indexvalues. After operations likepull(),shift(),splice(),$shift(),unshift(), positionedpush(),sort(), orreverse(), a subdoc can still think it lives at its previous array index. A later nested change then marks the wrong path, and since the delta serializes the value from whatever subdoc currently sits at that stale index, the update writes a no-op to the wrong element and the user's change is silently lost:This PR:
pull(),shift(),$shift(),splice(),unshift(), positionedpush(),sort(),reverse()push()on the fast path, the new subdoc is already cast with the correct appended index and existing indexes can't move, so repeated appends don't become O(n²)$shift(),unshift(),sort(),reverse(),pop(),$pop()reverse()register a full-array$set, matchingsort()/splice()/unshift()and avoiding stale pending atomics after a prior$push$getChanges()andmodifiedPaths()after each operation, persistence after save, populated-cache order, and reverse atomicsshift()was pointing at theunshiftarticleaddToSet(),nonAtomicPush(), andset()have the same populated cache gap, I'll send that as a follow-up PR once this one lands.