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

Cast array filters with embedded discriminators in arrays doesn't work #15053

Open
2 tasks done
jeffersonlipsky opened this issue Nov 22, 2024 · 1 comment
Open
2 tasks done
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@jeffersonlipsky
Copy link
Contributor

jeffersonlipsky commented Nov 22, 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

7.4.0

Node.js version

18.0

MongoDB server version

8.0

Typescript version (if applicable)

5.4.3

Description

When I made an update with arrayFilters in a schema with an embedded discriminators property the method castArrayFilters didn't know the discriminated properties. So Mongoose throws an error "Could not find path "pathDiscriminated" in schema"

Steps to Reproduce

So, You made an update with arrayFilters like the following:

model.updateManyWithDeleted(
        filter,
        { $set: 'items.$[a].ping': 'test'},
        { arrayFilters: ['a.kind': 'one'] },
      );

And you have a schema like the following

const ItemSchema = new Schema({ message: String }, { discriminatorKey: 'kind', _id: false });

const MainSchema = new Schema({ items: [ItemSchema] });

const path = MainSchema.path<MongooseSchema.Types.Array>('items');

const Onechema = new Schema({ping:String});
const TwoSchema = new Schema({pong:String});

path.discriminator('one', Onechema);
path.discriminator('two, TwoSchema);

Partial call stack:

at _castArrayFilters (../node_modules/mongoose/lib/helpers/update/castArrayFilters.js:100:15)
       at castArrayFilters (../node_modules/mongoose/lib/helpers/update/castArrayFilters.js:28:3)

Inside method getPath the method schema.path doesn't know the discriminated properties
Image
Image
Image

And the path method doesn't check in the discriminators
Image

Expected Behavior

The castArrayFilters, also check inside discriminators paths

@vkarpov15 vkarpov15 added this to the 8.8.4 milestone Nov 26, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro, the following script correctly sets items.$[a].ping to updated in Mongoose 7.4.0 and 8.8.3:

const mongoose = require('mongoose');

async function main() {
  await mongoose.connect('mongodb://localhost:27017/test');

  // Define the base schema for items
  const ItemSchema = new mongoose.Schema(
    { message: String },
    { discriminatorKey: 'kind', _id: false }
  );

  // Define the main schema that includes an array of items
  const MainSchema = new mongoose.Schema({ items: [ItemSchema] });

  // Add discriminators to the `items` path
  const OneSchema = new mongoose.Schema({ ping: String });
  const TwoSchema = new mongoose.Schema({ pong: String });

  const path = MainSchema.path('items');
  path.discriminator('one', OneSchema);
  path.discriminator('two', TwoSchema);

  // Create the model
  const MainModel = mongoose.model('Main', MainSchema);

  // Clean up previous test data
  await MainModel.deleteMany({});

  // Insert a sample document
  const doc = new MainModel({
    items: [ 
      { kind: 'one', message: 'Hello', ping: 'initial' },
      { kind: 'two', message: 'World', pong: 'data' },
    ],
  });

  await doc.save();

  console.log('Initial document:', await MainModel.findOne().lean());

  // Update with arrayFilters
  await MainModel.updateMany(
    {},
    { $set: { 'items.$[a].ping': 'updated' } },
    { arrayFilters: [{ 'a.kind': 'one' }] }
  );
  console.log('Updated document:', await MainModel.findOne().lean());

  console.log('Done');
  process.exit(0);
}

main().catch((err) => console.error(err));

Please modify the above script to demonstrate the issue you're seeing.

@vkarpov15 vkarpov15 added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Dec 4, 2024
@vkarpov15 vkarpov15 removed this from the 8.8.4 milestone Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants