@@ -1103,4 +1103,51 @@ describe('Map', function() {
11031103 assert . equal ( doc . addresses . get ( 'home' ) . length , 1 ) ;
11041104 assert . equal ( doc . addresses . get ( 'home' ) [ 0 ] . city , 'London' ) ;
11051105 } ) ;
1106+
1107+ it ( 'clears nested changes in subdocs (gh-15108)' , async function ( ) {
1108+ const CarSchema = new mongoose . Schema ( {
1109+ owners : {
1110+ type : Map ,
1111+ of : {
1112+ name : String
1113+ }
1114+ }
1115+ } ) ;
1116+ const CarModel = db . model ( 'Car' , CarSchema ) ;
1117+ const car = await CarModel . create ( {
1118+ owners : { abc : { name : 'John' } }
1119+ } ) ;
1120+
1121+ car . owners . get ( 'abc' ) . name = undefined ;
1122+ car . owners . delete ( 'abc' ) ;
1123+ assert . deepStrictEqual ( car . getChanges ( ) , { $unset : { 'owners.abc' : 1 } } ) ;
1124+ await car . save ( ) ;
1125+
1126+ const doc = await CarModel . findById ( car . _id ) ;
1127+ assert . strictEqual ( doc . owners . get ( 'abc' ) , undefined ) ;
1128+ } ) ;
1129+
1130+ it ( 'clears nested changes in doc arrays (gh-15108)' , async function ( ) {
1131+ const CarSchema = new mongoose . Schema ( {
1132+ owners : {
1133+ type : Map ,
1134+ of : [ {
1135+ _id : false ,
1136+ name : String
1137+ } ]
1138+ }
1139+ } ) ;
1140+ const CarModel = db . model ( 'Car' , CarSchema ) ;
1141+ const car = await CarModel . create ( {
1142+ owners : { abc : [ { name : 'John' } ] }
1143+ } ) ;
1144+
1145+ car . owners . get ( 'abc' ) [ 0 ] . name = undefined ;
1146+ car . owners . set ( 'abc' , [ { name : 'Bill' } ] ) ;
1147+ assert . deepStrictEqual ( car . getChanges ( ) , { $inc : { __v : 1 } , $set : { 'owners.abc' : [ { name : 'Bill' } ] } } ) ;
1148+ await car . save ( ) ;
1149+
1150+ const doc = await CarModel . findById ( car . _id ) ;
1151+ assert . deepStrictEqual ( doc . owners . get ( 'abc' ) . toObject ( ) , [ { name : 'Bill' } ] ) ;
1152+ } ) ;
11061153} ) ;
0 commit comments