You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
And the path method doesn't check in the discriminators
Expected Behavior
The castArrayFilters, also check inside discriminators paths
The text was updated successfully, but these errors were encountered:
I'm unable to repro, the following script correctly sets items.$[a].ping to updated in Mongoose 7.4.0 and 8.8.3:
constmongoose=require('mongoose');asyncfunctionmain(){awaitmongoose.connect('mongodb://localhost:27017/test');// Define the base schema for itemsconstItemSchema=newmongoose.Schema({message: String},{discriminatorKey: 'kind',_id: false});// Define the main schema that includes an array of itemsconstMainSchema=newmongoose.Schema({items: [ItemSchema]});// Add discriminators to the `items` pathconstOneSchema=newmongoose.Schema({ping: String});constTwoSchema=newmongoose.Schema({pong: String});constpath=MainSchema.path('items');path.discriminator('one',OneSchema);path.discriminator('two',TwoSchema);// Create the modelconstMainModel=mongoose.model('Main',MainSchema);// Clean up previous test dataawaitMainModel.deleteMany({});// Insert a sample documentconstdoc=newMainModel({items: [{kind: 'one',message: 'Hello',ping: 'initial'},{kind: 'two',message: 'World',pong: 'data'},],});awaitdoc.save();console.log('Initial document:',awaitMainModel.findOne().lean());// Update with arrayFiltersawaitMainModel.updateMany({},{$set: {'items.$[a].ping': 'updated'}},{arrayFilters: [{'a.kind': 'one'}]});console.log('Updated document:',awaitMainModel.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.
Prerequisites
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:
And you have a schema like the following
Partial call stack:
Inside method getPath the method schema.path doesn't know the discriminated properties
And the path method doesn't check in the discriminators
Expected Behavior
The castArrayFilters, also check inside discriminators paths
The text was updated successfully, but these errors were encountered: