@@ -46,7 +46,8 @@ the `Story` model.
46
46
<li ><a href =" #populate_multiple_documents " >Populating multiple existing documents</a ></li >
47
47
<li ><a href =" #deep-populate " >Populating across multiple levels</a ></li >
48
48
<li ><a href =" #cross-db-populate " >Populating across Databases</a ></li >
49
- <li ><a href =" #dynamic-ref " >Dynamic References via <code >refPath</code ></a ></li >
49
+ <li ><a href =" #dynamic-refpath " >Dynamic References via <code >refPath</code ></a ></li >
50
+ <li ><a href =" #dynamic-ref " >Dynamic References via <code >ref</code ></a ></li >
50
51
<li ><a href =" #populate-virtuals " >Populate Virtuals</a ></li >
51
52
<li ><a href =" #count " >Populate Virtuals: The Count Option</a ></li >
52
53
<li ><a href =" #match " >Populate Virtuals: The Match Option</a ></li >
@@ -469,7 +470,7 @@ const events = await Event.
469
470
populate ({ path: ' conversation' , model: Conversation });
470
471
```
471
472
472
- <h2 id =" dynamic-ref " ><a href =" #dynamic-ref " >Dynamic References via <code >refPath</code ></a ></h2 >
473
+ <h2 id =" dynamic-refpath " ><a href =" #dynamic-refpath " >Dynamic References via <code >refPath</code ></a ></h2 >
473
474
474
475
Mongoose can also populate from multiple collections based on the value
475
476
of a property in the document. Let's say you're building a schema for
@@ -561,6 +562,53 @@ also need an extra `populate()` call for every property, unless you use
561
562
Using ` refPath ` means you only need 2 schema paths and one ` populate() ` call
562
563
regardless of how many models your ` commentSchema ` can point to.
563
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.
566
+
567
+ ``` javascript
568
+ const commentSchema = new Schema ({
569
+ body: { type: String , required: true },
570
+ commentType: {
571
+ type: String ,
572
+ enum: [' comment' , ' review' ]
573
+ },
574
+ entityId: {
575
+ type: Schema .Types .ObjectId ,
576
+ required: true ,
577
+ refPath : function () {
578
+ return this .commentType === ' review' ? this .reviewEntityModel : this .commentEntityModel ; // 'this' refers to the document being populated
579
+ }
580
+ },
581
+ commentEntityModel: {
582
+ type: String ,
583
+ required: true ,
584
+ enum: [' BlogPost' , ' Review' ]
585
+ },
586
+ reviewEntityModel: {
587
+ type: String ,
588
+ required: true ,
589
+ enum: [' Vendor' , ' Product' ]
590
+ }
591
+ });
592
+ ```
593
+
594
+ <h2 id =" dynamic-ref " ><a href =" #dynamic-ref " >Dynamic References via <code >ref</code ></a ></h2 >
595
+
596
+ Just like ` refPath ` , ` ref ` can also be assigned a function.
597
+
598
+ ``` javascript
599
+ const commentSchema = new Schema ({
600
+ body: { type: String , required: true },
601
+ verifiedBuyer: Boolean
602
+ doc: {
603
+ type: Schema .Types .ObjectId ,
604
+ required: true ,
605
+ ref : function () {
606
+ return this .verifiedBuyer ? ' Product' : ' BlogPost' ; // 'this' refers to the document being populated
607
+ }
608
+ },
609
+ });
610
+ ```
611
+
564
612
<h2 id =" populate-virtuals " ><a href =" #populate-virtuals " >Populate Virtuals</a ></h2 >
565
613
566
614
So far you've only populated based on the ` _id ` field.
0 commit comments