Skip to content

fix(document): throw error if overwriting array selected with $slice - #16313

Merged
vkarpov15 merged 1 commit into
masterfrom
vkarpov15/gh-2432
Jun 3, 2026
Merged

fix(document): throw error if overwriting array selected with $slice#16313
vkarpov15 merged 1 commit into
masterfrom
vkarpov15/gh-2432

Conversation

@vkarpov15

@vkarpov15 vkarpov15 commented May 31, 2026

Copy link
Copy Markdown
Collaborator

Fix #2432

Summary

Currently, if you use a $elemMatch projection and try to save, Mongoose will throw a DivergentArrayError:

const user = await User.create({
    sessions: [{ token: 'one' }, { token: 'two' }]
  });

  const doc = await User.findById(user._id).select({
    sessions: { $elemMatch: { token: 'two' } }
  });

  doc.sessions.push({ token: 'three' });

  // Throws "For your own good, using `document.save()` to update an array which was selected using an $elemMatch or $slice projection OR populated using skip, limit, query conditions, or exclusion of the _id field when the operation results in a $pop or $set of the entire array is not supported. The following path(s) would have been modified unsafely:
  sessions"
  await doc.save().then(() => null, err => err);

The crux of #2432 is we should do that for $slice as well. But only for $set atomics - if save() would just $push or $pull or $pop from the array then that won't overwrite the existing array.

Examples

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Extends divergent-array detection so document.save() throws DivergentArrayError when an array selected via a $slice projection would be overwritten via a $set atomic, while allowing safe $push/$pull/$pop/$addToSet updates. Fixes #2432.

Changes:

  • checkDivergentArray() now also flags arrays selected with $slice when modifications resolve to a $set atomic.
  • DivergentArrayError message updated to mention $slice.
  • Adds tests covering $set, $push, $pull, $pop, $addToSet behavior on $slice-selected arrays.

Reviewed changes

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

File Description
lib/document.js Adds $slice projection check in checkDivergentArray() returning the top path when atomics contain $set.
lib/error/divergentArray.js Updates error message to mention $slice projection.
test/model.field.selection.test.js Adds tests verifying divergent error for $set and successful saves for $push/$pull/$pop/$addToSet.

@vkarpov15
vkarpov15 merged commit f6ef2c8 into master Jun 3, 2026
51 checks passed
@hasezoey
hasezoey deleted the vkarpov15/gh-2432 branch June 4, 2026 09:00
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.

Setting fields on nested schema arrays causes entire array to be marked as modified.

3 participants