@@ -33,6 +33,10 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
33
33
return ;
34
34
}
35
35
36
+ // Map to store discriminator values for embedded documents in the array filters.
37
+ // This is used to handle cases where array filters target specific embedded document types.
38
+ const discriminatorValueMap = { } ;
39
+
36
40
for ( const filter of arrayFilters ) {
37
41
if ( filter == null ) {
38
42
throw new Error ( `Got null array filter in ${ arrayFilters } ` ) ;
@@ -58,12 +62,13 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
58
62
updatedPathsByFilter [ filterWildcardPath ]
59
63
) ;
60
64
61
- const baseSchematype = getPath ( schema , baseFilterPath ) ;
65
+ const baseSchematype = getPath ( schema , baseFilterPath , discriminatorValueMap ) ;
62
66
let filterBaseSchema = baseSchematype != null ? baseSchematype . schema : null ;
63
67
if ( filterBaseSchema != null &&
64
68
filterBaseSchema . discriminators != null &&
65
69
filter [ filterWildcardPath + '.' + filterBaseSchema . options . discriminatorKey ] ) {
66
70
filterBaseSchema = filterBaseSchema . discriminators [ filter [ filterWildcardPath + '.' + filterBaseSchema . options . discriminatorKey ] ] || filterBaseSchema ;
71
+ discriminatorValueMap [ baseFilterPath ] = filter [ filterWildcardPath + '.' + filterBaseSchema . options . discriminatorKey ] ;
67
72
}
68
73
69
74
for ( const key of keys ) {
@@ -83,7 +88,7 @@ function _castArrayFilters(arrayFilters, schema, strictQuery, updatedPathsByFilt
83
88
// If there are multiple array filters in the path being updated, make sure
84
89
// to replace them so we can get the schema path.
85
90
filterPathRelativeToBase = cleanPositionalOperators ( filterPathRelativeToBase ) ;
86
- schematype = getPath ( filterBaseSchema , filterPathRelativeToBase ) ;
91
+ schematype = getPath ( filterBaseSchema , filterPathRelativeToBase , discriminatorValueMap ) ;
87
92
}
88
93
89
94
if ( schematype == null ) {
0 commit comments