@@ -13041,6 +13041,51 @@ describe('document', function() {
1304113041 assert . ok ( doc . docArr . toString ( ) . includes ( 'child' ) , doc . docArr . toString ( ) ) ;
1304213042 assert . ok ( doc . docArr . toString ( ) . includes ( 'test child' ) , doc . docArr . toString ( ) ) ;
1304313043 } ) ;
13044+
13045+ it ( 'minimizes when updating existing documents (gh-13782)' , async function ( ) {
13046+ const schema = new Schema ( {
13047+ metadata : {
13048+ type : { } ,
13049+ default : { } ,
13050+ required : true ,
13051+ _id : false
13052+ }
13053+ } , { minimize : true } ) ;
13054+ const Model = db . model ( 'Test' , schema ) ;
13055+ const m = new Model ( { metadata : { } } ) ;
13056+ await m . save ( ) ;
13057+
13058+ const x = await Model . findById ( m . _id ) . exec ( ) ;
13059+ x . metadata = { } ;
13060+ await x . save ( ) ;
13061+
13062+ const { metadata } = await Model . findById ( m . _id ) . orFail ( ) ;
13063+ assert . strictEqual ( metadata , null ) ;
13064+ } ) ;
13065+
13066+ it ( 'saves when setting subdocument to empty object (gh-14420) (gh-13782)' , async function ( ) {
13067+ const SubSchema = new mongoose . Schema ( {
13068+ name : { type : String } ,
13069+ age : Number
13070+ } , { _id : false } ) ;
13071+
13072+ const MainSchema = new mongoose . Schema ( {
13073+ sub : {
13074+ type : SubSchema
13075+ }
13076+ } ) ;
13077+
13078+ const MainModel = db . model ( 'Test' , MainSchema ) ;
13079+
13080+ const doc = new MainModel ( { sub : { name : 'Hello World' , age : 42 } } ) ;
13081+ await doc . save ( ) ;
13082+
13083+ doc . sub = { } ;
13084+ await doc . save ( ) ;
13085+
13086+ const savedDoc = await MainModel . findById ( doc . id ) . orFail ( ) ;
13087+ assert . strictEqual ( savedDoc . sub , null ) ;
13088+ } ) ;
1304413089} ) ;
1304513090
1304613091describe ( 'Check if instance function that is supplied in schema option is availabe' , function ( ) {
0 commit comments