@@ -554,67 +554,43 @@ comments[0].product.name; // "The Count of Monte Cristo"
554
554
comments[1 ].blogPost .title ; // "Top 10 French Novels"
555
555
```
556
556
557
- You could also assign a function to ` refPath ` , making it so that it
558
- selects a refpath depending on a value on the document being populated. For example.
557
+ Defining separate ` blogPost ` and ` product ` properties works for this simple
558
+ example. But, if you decide to allow users to also comment on articles or
559
+ other comments, you'll need to add more properties to your schema. You'll
560
+ also need an extra ` populate() ` call for every property, unless you use
561
+ [ mongoose-autopopulate] ( https://www.npmjs.com/package/mongoose-autopopulate ) .
562
+ Using ` refPath ` means you only need 2 schema paths and one ` populate() ` call
563
+ regardless of how many models your ` commentSchema ` can point to.
564
+
565
+ You could also assign a function to ` refPath ` , which means Mongoose selects a refPath depending on a value on the document being populated.
559
566
560
567
``` javascript
561
568
const commentSchema = new Schema ({
562
569
body: { type: String , required: true },
563
- verifiedBuyer: Boolean ,
564
- doc: {
570
+ commentType: {
571
+ type: String ,
572
+ enum: [' comment' , ' review' ]
573
+ },
574
+ entityId: {
565
575
type: Schema .Types .ObjectId ,
566
576
required: true ,
567
577
refPath : function () {
568
- return this .verifiedBuyer ? this .otherOption : this .docModel ; // 'this' refers to the document being populated
578
+ return this .commentType === ' review ' ? this .reviewEntityModel : this .commentEntityModel ; // 'this' refers to the document being populated
569
579
}
570
580
},
571
- docModel : {
581
+ commentEntityModel : {
572
582
type: String ,
573
583
required: true ,
574
584
enum: [' BlogPost' , ' Review' ]
575
585
},
576
- otherOption : {
586
+ reviewEntityModel : {
577
587
type: String ,
578
588
required: true ,
579
589
enum: [' Vendor' , ' Product' ]
580
590
}
581
591
});
582
-
583
- const Product = mongoose .model (' Product' , new Schema ({ name: String }));
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 }));
587
- const Comment = mongoose .model (' Comment' , commentSchema);
588
-
589
- const book = await Product .create ({ name: ' The Count of Monte Cristo' });
590
- const post = await BlogPost .create ({ title: ' Top 10 French Novels' });
591
-
592
- const commentOnBook = await Comment .create ({
593
- body: ' Great read' ,
594
- verifiedBuyer: true
595
- doc: book ._id ,
596
- docModel: ' Review' ,
597
- otherOption: ' Product'
598
- });
599
-
600
- const commentOnPost = await Comment .create ({
601
- body: ' Very informative' ,
602
- verifiedBuyer: false ,
603
- doc: post ._id ,
604
- docModel: ' BlogPost' ,
605
- otherOption: ' Vendor'
606
- });
607
-
608
592
```
609
593
610
- Defining separate ` blogPost ` and ` product ` properties works for this simple
611
- example. But, if you decide to allow users to also comment on articles or
612
- other comments, you'll need to add more properties to your schema. You'll
613
- also need an extra ` populate() ` call for every property, unless you use
614
- [ mongoose-autopopulate] ( https://www.npmjs.com/package/mongoose-autopopulate ) .
615
- Using ` refPath ` means you only need 2 schema paths and one ` populate() ` call
616
- regardless of how many models your ` commentSchema ` can point to.
617
-
618
594
<h2 id =" dynamic-ref " ><a href =" #dynamic-ref " >Dynamic References via <code >ref</code ></a ></h2 >
619
595
620
596
Just like ` refPath ` , ` ref ` can also be assigned a function.
@@ -631,10 +607,6 @@ const commentSchema = new Schema({
631
607
}
632
608
},
633
609
});
634
-
635
- const Product = mongoose .model (' Product' , new Schema ({ name: String }));
636
- const BlogPost = mongoose .model (' BlogPost' , new Schema ({ title: String }));
637
- const Comment = mongoose .model (' Comment' , commentSchema);
638
610
```
639
611
640
612
<h2 id =" populate-virtuals " ><a href =" #populate-virtuals " >Populate Virtuals</a ></h2 >
0 commit comments