Skip to content

fix(query): use embedded discriminator schema when filter selects the key with $eq or $in - #16543

Open
breken-ai wants to merge 1 commit into
Automattic:masterfrom
breken-ai:fix/embedded-discriminator-eq-filter
Open

breken-ai wants to merge 1 commit into
Automattic:masterfrom
breken-ai:fix/embedded-discriminator-eq-filter

Conversation

@breken-ai

Copy link
Copy Markdown

Summary

When the filter picks an embedded discriminator with $eq or a single-value $in instead 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: the updateOne() call resolves without an error, but nothing is written.

getEmbeddedDiscriminatorPath() reads the discriminator value from filter['events.kind'] or filter.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 drops events.$.quantity. Query casting has the same gap: cast() already accepts a single-value $in for embedded discriminators, but $eq leaves 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 $in still falls back to the base schema as before. The helper is used:

  • in getEmbeddedDiscriminatorPath() for the dotted filter path and the $elemMatch key (update casting),
  • in cast() for the embedded discriminator lookup, replacing the inline $in special case,
  • in 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.js fails on master (modifiedCount is undefined because the update is dropped) and passes with this change. The full suite passes locally (4584 passing, 47 pending, mongodb-memory-server), and eslint is clean on the changed files.

Examples

const eventSchema = new Schema({ message: String }, { discriminatorKey: 'kind', _id: false });
const schema = new Schema({ events: [eventSchema] });
schema.path('events').discriminator('Purchased', new Schema({ quantity: Number }, { _id: false }));
const Test = mongoose.model('Test', schema);

const doc = await Test.create({ events: [{ kind: 'Purchased', quantity: 1 }] });

// Works today
await Test.updateOne({ _id: doc._id, 'events.kind': 'Purchased' }, { $inc: { 'events.$.quantity': 2 } });

// Before: the update is cast to {} and nothing is written. After: quantity is incremented
await Test.updateOne({ _id: doc._id, 'events.kind': { $eq: 'Purchased' } }, { $inc: { 'events.$.quantity': 2 } });
await Test.updateOne({ _id: doc._id, events: { $elemMatch: { kind: { $eq: 'Purchased' } } } }, { $inc: { 'events.$.quantity': 2 } });

I found and fixed this with help from an AI coding assistant, then checked the change and the test results myself.

… key with $eq or $in

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant