@@ -3091,6 +3091,58 @@ describe('model: updateOne: ', function() {
30913091 assert . equal ( doc . login . keys . length , 1 ) ;
30923092 assert . equal ( doc . login . keys [ 0 ] . id , 'test2' ) ;
30933093 } ) ;
3094+
3095+ it ( 'casts using overwritten discriminator key schema (gh-15051)' , async function ( ) {
3096+ const embedDiscriminatorSchema = new mongoose . Schema ( {
3097+ field1 : String
3098+ } ) ;
3099+ const embedDiscriminatorSchema2 = new mongoose . Schema ( {
3100+ field2 : String
3101+ } ) ;
3102+ const embedSchema = new mongoose . Schema ( {
3103+ field : String ,
3104+ key : String
3105+ } , { discriminatorKey : 'key' } ) ;
3106+ embedSchema . discriminator ( 'Type1' , embedDiscriminatorSchema ) ;
3107+ embedSchema . discriminator ( 'Type2' , embedDiscriminatorSchema2 ) ;
3108+
3109+ const testSchema = new mongoose . Schema ( {
3110+ testArray : [ embedSchema ]
3111+ } ) ;
3112+
3113+ const TestModel = db . model ( 'Test' , testSchema ) ;
3114+ const test = new TestModel ( {
3115+ testArray : [ {
3116+ key : 'Type1' ,
3117+ field : 'field' ,
3118+ field1 : 'field1'
3119+ } ]
3120+ } ) ;
3121+ await test . save ( ) ;
3122+
3123+ const field2update = 'field2 update' ;
3124+ await TestModel . updateOne (
3125+ { _id : test . _id } ,
3126+ {
3127+ $set : {
3128+ 'testArray.$[element].key' : 'Type2' ,
3129+ 'testArray.$[element].field2' : field2update
3130+ }
3131+ } ,
3132+ {
3133+ arrayFilters : [
3134+ {
3135+ 'element._id' : test . testArray [ 0 ] . _id
3136+ }
3137+ ] ,
3138+ overwriteDiscriminatorKey : true
3139+ }
3140+ ) ;
3141+
3142+ const r2 = await TestModel . findById ( test . _id ) ;
3143+ assert . equal ( r2 . testArray [ 0 ] . key , 'Type2' ) ;
3144+ assert . equal ( r2 . testArray [ 0 ] . field2 , field2update ) ;
3145+ } ) ;
30943146} ) ;
30953147
30963148async function delay ( ms ) {
0 commit comments