@@ -360,4 +360,78 @@ describe('bug fixes', function() {
360360 section = await Section . findById ( section ) ;
361361 assert . equal ( section . subdoc . subSection . name , 'foo' ) ;
362362 } ) ;
363+
364+ it ( 'supports match as a function in virtuals (gh-112)' , async function ( ) {
365+ const teacherSchema = new Schema ( {
366+ schoolClass : {
367+ type : Schema . Types . ObjectId ,
368+ ref : 'gh112_SchoolClass' ,
369+ autopopulate : { maxDepth : 2 }
370+ } ,
371+ schoolSubject : {
372+ type : Schema . Types . ObjectId ,
373+ ref : 'gh112_SchoolSubject' ,
374+ autopopulate : { maxDepth : 2 }
375+ }
376+ } ) ;
377+
378+ const lectureSchema = new Schema ( {
379+ schoolClass : {
380+ type : Schema . Types . ObjectId ,
381+ ref : 'gh112_SchoolClass' ,
382+ autopopulate : { maxDepth : 2 }
383+ } ,
384+ schoolSubject : {
385+ type : Schema . Types . ObjectId ,
386+ ref : 'gh112_SchoolSubject' ,
387+ autopopulate : { maxDepth : 2 }
388+ }
389+ } ) ;
390+
391+ lectureSchema . virtual ( 'assignedTeacher' , {
392+ ref : 'gh112_Teacher' ,
393+ localField : 'schoolClass' ,
394+ foreignField : 'schoolClass' ,
395+ autopopulate : { maxDepth : 2 } ,
396+ match : ( lecture ) => ( {
397+ schoolSubject : lecture . schoolSubject
398+ } ) ,
399+ justOne : true
400+ } ) ;
401+
402+ teacherSchema . plugin ( autopopulate ) ;
403+ lectureSchema . plugin ( autopopulate ) ;
404+
405+ const Teacher = db . model ( 'gh112_Teacher' , teacherSchema ) ;
406+ const Lecture = db . model ( 'gh112_Lecture' , lectureSchema ) ;
407+ const SchoolClass = db . model ( 'gh112_SchoolClass' , new Schema ( { name : String } ) ) ;
408+ const SchoolSubject = db . model ( 'gh112_SchoolSubject' , new Schema ( { name : String } ) ) ;
409+
410+ // Clean up any leftover data from previous runs
411+ await Teacher . deleteMany ( { } ) ;
412+ await Lecture . deleteMany ( { } ) ;
413+ await SchoolClass . deleteMany ( { } ) ;
414+ await SchoolSubject . deleteMany ( { } ) ;
415+
416+ const schoolClass = await SchoolClass . create ( { name : 'MTH-101' } ) ;
417+ const schoolSubject = await SchoolSubject . create ( { name : 'Math' } ) ;
418+
419+ await Teacher . create ( {
420+ schoolClass : schoolClass . _id ,
421+ schoolSubject : schoolSubject . _id
422+ } ) ;
423+
424+ await Lecture . create ( {
425+ schoolClass : schoolClass . _id ,
426+ schoolSubject : schoolSubject . _id
427+ } ) ;
428+
429+ const lecture = await Lecture . findOne ( ) ;
430+ assert . ok ( lecture . assignedTeacher ) ;
431+ // schoolClass/schoolSubject may be populated documents or ObjectIds depending on Mongoose version
432+ const teacherSchoolClassId = lecture . assignedTeacher . schoolClass . _id || lecture . assignedTeacher . schoolClass ;
433+ const teacherSchoolSubjectId = lecture . assignedTeacher . schoolSubject . _id || lecture . assignedTeacher . schoolSubject ;
434+ assert . equal ( teacherSchoolClassId . toString ( ) , schoolClass . _id . toString ( ) ) ;
435+ assert . equal ( teacherSchoolSubjectId . toString ( ) , schoolSubject . _id . toString ( ) ) ;
436+ } ) ;
363437} ) ;
0 commit comments