Conversation
… key with $eq or $in Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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
When the filter picks an embedded discriminator with
$eqor a single-value$ininstead of a bare value, Mongoose casts with the base subdocument schema. Discriminator-only paths in the update then count as unknown and strict mode removes them. In the positional-update example below, the whole update is dropped: theupdateOne()call resolves without an error, but nothing is written.getEmbeddedDiscriminatorPath()reads the discriminator value fromfilter['events.kind']orfilter.events.$elemMatch.kind, and only works when that value is the bare string.{ $eq: 'Purchased' }is a common way to write the same condition (for example, code that always wraps user input in$eq). With it,getDiscriminatorByValue()finds nothing, so strict mode dropsevents.$.quantity. Query casting has the same gap:cast()already accepts a single-value$infor embedded discriminators, but$eqleaves discriminator-only paths uncast ('3'stays a string and matches nothing).This PR adds a small helper,
getDiscriminatorValueFromFilter(). It turns'X',{ $eq: 'X' }and{ $in: ['X'] }into'X'and returns anything else unchanged, so a multi-value$instill falls back to the base schema as before. The helper is used:getEmbeddedDiscriminatorPath()for the dotted filter path and the$elemMatchkey (update casting),cast()for the embedded discriminator lookup, replacing the inline$inspecial case,cast()where it picks a discriminator schema from the filter's discriminator key, so$elemMatch: { kind: { $eq: 'X' }, ... }casts with the discriminator schema.Tests: the new test in
test/model.updateOne.test.jsfails onmaster(modifiedCountisundefinedbecause the update is dropped) and passes with this change. The full suite passes locally (4584 passing, 47 pending, mongodb-memory-server), andeslintis clean on the changed files.Examples
I found and fixed this with help from an AI coding assistant, then checked the change and the test results myself.