@@ -4125,6 +4125,55 @@ describe('Model', function() {
41254125 assert . equal ( err . validationErrors [ 0 ] . errors [ 'num' ] . name , 'CastError' ) ;
41264126 } ) ;
41274127
4128+ it ( 'handles array filters (gh-14978)' , async function ( ) {
4129+ const embedDiscriminatorSchema = new mongoose . Schema ( {
4130+ field1 : String
4131+ } ) ;
4132+
4133+ const embedSchema = new mongoose . Schema ( {
4134+ field : String ,
4135+ key : String
4136+ } , { discriminatorKey : 'key' } ) ;
4137+ embedSchema . discriminator ( 'Type1' , embedDiscriminatorSchema ) ;
4138+
4139+ const testSchema = new mongoose . Schema ( {
4140+ testArray : [ embedSchema ]
4141+ } ) ;
4142+ const TestModel = db . model ( 'Test' , testSchema ) ;
4143+
4144+ const test = new TestModel ( {
4145+ testArray : [ {
4146+ key : 'Type1' ,
4147+ field : 'field' ,
4148+ field1 : 'field1'
4149+ } ]
4150+ } ) ;
4151+ const r1 = await test . save ( ) ;
4152+ assert . equal ( r1 . testArray [ 0 ] . field1 , 'field1' ) ;
4153+
4154+ const field1update = 'field1 update' ;
4155+ await TestModel . bulkWrite ( [ {
4156+ updateOne : {
4157+ filter : { _id : r1 . _id } ,
4158+ update : {
4159+ $set : {
4160+ 'testArray.$[element].field1' : field1update ,
4161+ 'testArray.$[element].nonexistentProp' : field1update
4162+ }
4163+ } ,
4164+ arrayFilters : [
4165+ {
4166+ 'element._id' : r1 . testArray [ 0 ] . _id ,
4167+ 'element.key' : 'Type1'
4168+ }
4169+ ]
4170+ }
4171+ } ] ) ;
4172+ const r2 = await TestModel . findById ( r1 . _id ) . lean ( ) ;
4173+ assert . equal ( r2 . testArray [ 0 ] . field1 , field1update ) ;
4174+ assert . strictEqual ( r2 . testArray [ 0 ] . nonexistentProp , undefined ) ;
4175+ } ) ;
4176+
41284177 it ( 'with child timestamps and array filters (gh-7032)' , async function ( ) {
41294178 const childSchema = new Schema ( { name : String } , { timestamps : true } ) ;
41304179
0 commit comments