@@ -8254,11 +8254,68 @@ describe('model: populate:', function() {
8254
8254
path : 'companyId' ,
8255
8255
justOne : true
8256
8256
}
8257
- } ) ;
8257
+ } ) . lean ( ) ;
8258
+ console . log ( populatedRides )
8258
8259
assert . deepEqual ( populatedRides [ 0 ] . files , [ ] ) ;
8259
8260
assert . deepEqual ( populatedRides [ 1 ] . files , [ ] ) ;
8260
8261
} ) ;
8261
8262
8263
+ it ( 'doesnt insert empty document when lean populating a path within an underneath non-existent document array (gh-14098)' , async function ( ) {
8264
+ const userSchema = new mongoose . Schema ( {
8265
+ fullName : String ,
8266
+ company : String
8267
+ } ) ;
8268
+ const User = db . model ( 'User' , userSchema ) ;
8269
+
8270
+ const fileSchema = new mongoose . Schema ( {
8271
+ _id : String ,
8272
+ uploaderId : {
8273
+ type : mongoose . ObjectId ,
8274
+ ref : 'User'
8275
+ }
8276
+ } , { toObject : { virtuals : true } , toJSON : { virtuals : true } } ) ;
8277
+ fileSchema . virtual ( 'uploadedBy' , {
8278
+ ref : 'User' ,
8279
+ localField : 'uploaderId' ,
8280
+ foreignField : '_id' ,
8281
+ justOne : true
8282
+ } ) ;
8283
+
8284
+ const contentSchema = new mongoose . Schema ( {
8285
+ memo : String ,
8286
+ files : { type : [ fileSchema ] , default : [ ] }
8287
+ } , { toObject : { virtuals : true } , toJSON : { virtuals : true } , _id : false } ) ;
8288
+
8289
+ const postSchema = new mongoose . Schema ( {
8290
+ title : String ,
8291
+ content : { type : contentSchema }
8292
+ } , { toObject : { virtuals : true } , toJSON : { virtuals : true } } ) ;
8293
+ const Post = db . model ( 'Test1' , postSchema ) ;
8294
+
8295
+ const user = await User . create ( { fullName : 'John Doe' , company : 'GitHub' } ) ;
8296
+ await Post . create ( [
8297
+ { title : 'London-Paris' } ,
8298
+ {
8299
+ title : 'Berlin-Moscow' ,
8300
+ content : {
8301
+ memo : 'Not Easy' ,
8302
+ files : [ { _id : '123' , uploaderId : user . _id } ]
8303
+ }
8304
+ }
8305
+ ] ) ;
8306
+ await Post . updateMany ( { } , { $unset : { 'content.files' : 1 } } ) ;
8307
+ const populatedRides = await Post . find ( { } ) . populate ( {
8308
+ path : 'content.files.uploadedBy' ,
8309
+ justOne : true
8310
+ } ) . lean ( ) ;
8311
+
8312
+ console . log ( populatedRides [ 0 ] . content ) ;
8313
+ console . log ( populatedRides [ 1 ] . content ) ;
8314
+
8315
+ assert . equal ( populatedRides [ 0 ] . content . files , undefined ) ;
8316
+ assert . equal ( populatedRides [ 1 ] . content . files , undefined ) ;
8317
+ } )
8318
+
8262
8319
it ( 'sets empty array if populating undefined path (gh-8455)' , async function ( ) {
8263
8320
const TestSchema = new Schema ( {
8264
8321
thingIds : [ mongoose . ObjectId ]
0 commit comments