@@ -10565,4 +10565,41 @@ describe('model: populate:', function() {
10565
10565
assert . equal ( populatedBooks [ 0 ] . author . name , 'Author1' ) ;
10566
10566
} ) ;
10567
10567
} ) ;
10568
+
10569
+ it ( 'avoids setting empty array on lean document when populate result is undefined (gh-10599)' , function ( ) {
10570
+ return co ( function * ( ) {
10571
+ const ImageSchema = Schema ( { imageName : String } , { _id : false } ) ;
10572
+ const TextSchema = Schema ( {
10573
+ textName : String ,
10574
+ attached : [ { type : 'ObjectId' , ref : 'Test2' } ]
10575
+ } , { _id : false } ) ;
10576
+
10577
+ const ItemSchema = Schema ( { objectType : String } , {
10578
+ discriminatorKey : 'objectType' ,
10579
+ _id : false
10580
+ } ) ;
10581
+
10582
+ const ExampleSchema = Schema ( { test : String , list : [ ItemSchema ] } ) ;
10583
+
10584
+ ExampleSchema . path ( 'list' ) . discriminator ( 'Image' , ImageSchema ) ;
10585
+ ExampleSchema . path ( 'list' ) . discriminator ( 'Text' , TextSchema ) ;
10586
+ const Example = db . model ( 'Test' , ExampleSchema ) ;
10587
+ const Another = db . model ( 'Test2' , Schema ( { test : 'String' } ) ) ;
10588
+
10589
+ yield Another . create ( { _id : '61254490ea89de0004c8f2a0' , test : 'test' } ) ;
10590
+
10591
+ const example = yield Example . create ( {
10592
+ test : 'example' ,
10593
+ list : [
10594
+ { imageName : 'an image' , objectType : 'Image' } ,
10595
+ { textName : 'this is a text' , attached : [ '61254490ea89de0004c8f2a0' ] , objectType : 'Text' }
10596
+ ]
10597
+ } ) ;
10598
+ const query = Example . findById ( example . _id ) . populate ( 'list.attached' ) . lean ( ) ;
10599
+
10600
+ const result = yield query . exec ( ) ;
10601
+ assert . strictEqual ( result . list [ 0 ] . attached , void 0 ) ;
10602
+ assert . equal ( result . list [ 1 ] . attached [ 0 ] . test , 'test' ) ;
10603
+ } ) ;
10604
+ } ) ;
10568
10605
} ) ;
0 commit comments