@@ -1115,4 +1115,41 @@ describe('document.populate', function() {
11151115 assert . equal ( docD . refB . title , 'test 2' ) ;
11161116 assert . equal ( docD . refC . content , 'test 3' ) ;
11171117 } ) ;
1118+
1119+ it ( 'handles re-populating map of array of refs (gh-9359)' , async function ( ) {
1120+ const UserSchema = mongoose . Schema ( {
1121+ columns : { type : Map , of : [ { type : 'ObjectId' , ref : 'Test1' } ] }
1122+ } ) ;
1123+ const CardSchema = mongoose . Schema ( {
1124+ title : { type : String } ,
1125+ sequence : { type : 'ObjectId' , ref : 'Test2' }
1126+ } ) ;
1127+ const SequenceSchema = mongoose . Schema ( {
1128+ foo : { type : String }
1129+ } ) ;
1130+
1131+ const Sequence = db . model ( 'Test2' , SequenceSchema ) ;
1132+ const Card = db . model ( 'Test1' , CardSchema ) ;
1133+ const User = db . model ( 'Test' , UserSchema ) ;
1134+
1135+ const sequence = await Sequence . create ( { foo : 'bar' } ) ;
1136+ const card1 = await Card . create ( { title : 'card1' , sequence } ) ;
1137+ const card2 = await Card . create ( { title : 'card2' , sequence } ) ;
1138+ const card3 = await Card . create ( { title : 'card3' } ) ;
1139+ const card4 = await Card . create ( { title : 'card4' , sequence } ) ;
1140+ await User . create ( {
1141+ columns : { key1 : [ card1 , card2 ] , key2 : [ card3 , card4 ] }
1142+ } ) ;
1143+
1144+ const user = await User . findOne ( ) ;
1145+ await user . populate ( 'columns.$*' ) ;
1146+ assert . deepStrictEqual ( user . columns . get ( 'key1' ) . map ( subdoc => subdoc . title ) , [ 'card1' , 'card2' ] ) ;
1147+ assert . deepStrictEqual ( user . columns . get ( 'key2' ) . map ( subdoc => subdoc . title ) , [ 'card3' , 'card4' ] ) ;
1148+ await user . populate ( 'columns.$*.sequence' ) ;
1149+ assert . deepStrictEqual ( user . columns . get ( 'key1' ) . map ( subdoc => subdoc . title ) , [ 'card1' , 'card2' ] ) ;
1150+ assert . deepStrictEqual ( user . columns . get ( 'key1' ) . map ( subdoc => subdoc . sequence . foo ) , [ 'bar' , 'bar' ] ) ;
1151+ assert . deepStrictEqual ( user . columns . get ( 'key2' ) . map ( subdoc => subdoc . title ) , [ 'card3' , 'card4' ] ) ;
1152+ assert . deepStrictEqual ( user . columns . get ( 'key2' ) . map ( subdoc => subdoc . sequence ?. foo ) , [ undefined , 'bar' ] ) ;
1153+
1154+ } ) ;
11181155} ) ;
0 commit comments