Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use $pull on nested array within discriminated union #14675

Closed
2 tasks done
rpmccarter opened this issue Jun 18, 2024 · 0 comments · Fixed by #14676
Closed
2 tasks done

Unable to use $pull on nested array within discriminated union #14675

rpmccarter opened this issue Jun 18, 2024 · 0 comments · Fixed by #14676
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@rpmccarter
Copy link

rpmccarter commented Jun 18, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.4.3

Node.js version

20.10.0

MongoDB server version

6.0.14

Typescript version (if applicable)

5.4.5

Description

It is not possible to perform a $pull on a nested discriminator field using Model.findOneAndUpdate()

Steps to Reproduce

I have created an MRE repo at rpmccarter/mongoose-discriminator-pull-bug-repro that demonstrates the bug. Here is some of the relevant code, taken from index.ts:

const LoginSchema = new Schema({}, { discriminatorKey: 'type', _id: false });
const UserSchema = new Schema({
  name: String,
  login: LoginSchema,
});

UserSchema.path<Schema.Types.Subdocument>('login').discriminator(
  'ssh-key',
  new Schema(
    {
      keys: {
        type: [{
          id: { type: String, required: true },
          publicKey: { type: String, required: true }
        }],
        default: [],
      }
    },
    { _id: false }
  )
);

const User = mongoose.model<User>('User', UserSchema, 'users');

const id = '123-456';

// does not work
await User.findOneAndUpdate(
  { _id: user._id, 'login.type': 'ssh-key' },
  { $pull: { 'login.keys': { id } } },
  { new: true }
);

Expected Behavior

$pull should work on nested array fields when the discriminator is specified.

@vkarpov15 vkarpov15 added this to the 8.4.4 milestone Jun 19, 2024
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jun 19, 2024
vkarpov15 added a commit that referenced this issue Jun 19, 2024
vkarpov15 added a commit that referenced this issue Jun 21, 2024
fix(query): cast $pull using embedded discriminator schema when discriminator key is set in filter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants