@@ -560,37 +560,49 @@ selects a refpath depending on a value on the document being populated. For exam
560
560
``` javascript
561
561
const commentSchema = new Schema ({
562
562
body: { type: String , required: true },
563
+ verifiedBuyer: Boolean ,
563
564
doc: {
564
565
type: Schema .Types .ObjectId ,
565
566
required: true ,
566
567
refPath : function () {
567
- return this .docModel ; // 'this' refers to the document being populated
568
+ return this .verifiedBuyer ? this . otherOption : this . docModel ; // 'this' refers to the document being populated
568
569
}
569
570
},
570
571
docModel: {
571
572
type: String ,
572
573
required: true ,
573
- enum: [' BlogPost' , ' Product' ]
574
+ enum: [' BlogPost' , ' Review' ]
575
+ },
576
+ otherOption: {
577
+ type: String ,
578
+ required: true ,
579
+ enum: [' Vendor' , ' Product' ]
574
580
}
575
581
});
576
582
577
583
const Product = mongoose .model (' Product' , new Schema ({ name: String }));
578
584
const BlogPost = mongoose .model (' BlogPost' , new Schema ({ title: String }));
585
+ const Vendor = mongoose .model (' Vendor' , new Schema ({ productType: String }));
586
+ const Review = mongoose .model (' Review' , new Schema ({ rating: String }));
579
587
const Comment = mongoose .model (' Comment' , commentSchema);
580
588
581
589
const book = await Product .create ({ name: ' The Count of Monte Cristo' });
582
590
const post = await BlogPost .create ({ title: ' Top 10 French Novels' });
583
591
584
592
const commentOnBook = await Comment .create ({
585
593
body: ' Great read' ,
594
+ verifiedBuyer: true
586
595
doc: book ._id ,
587
- docModel: ' Product'
596
+ docModel: ' Review' ,
597
+ otherOption: ' Product'
588
598
});
589
599
590
600
const commentOnPost = await Comment .create ({
591
601
body: ' Very informative' ,
602
+ verifiedBuyer: false ,
592
603
doc: post ._id ,
593
- docModel: ' BlogPost'
604
+ docModel: ' BlogPost' ,
605
+ otherOption: ' Vendor'
594
606
});
595
607
596
608
```
0 commit comments