Skip to content

Commit b9f29f0

Browse files
authored
Merge pull request #14133 from Automattic/IslandRhythms/ref-function-docs
Island rhythms/ref function docs
2 parents 626762a + ce4487c commit b9f29f0

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

docs/populate.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ the `Story` model.
4646
<li><a href="#populate_multiple_documents">Populating multiple existing documents</a></li>
4747
<li><a href="#deep-populate">Populating across multiple levels</a></li>
4848
<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>
5051
<li><a href="#populate-virtuals">Populate Virtuals</a></li>
5152
<li><a href="#count">Populate Virtuals: The Count Option</a></li>
5253
<li><a href="#match">Populate Virtuals: The Match Option</a></li>
@@ -469,7 +470,7 @@ const events = await Event.
469470
populate({ path: 'conversation', model: Conversation });
470471
```
471472

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>
473474

474475
Mongoose can also populate from multiple collections based on the value
475476
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
561562
Using `refPath` means you only need 2 schema paths and one `populate()` call
562563
regardless of how many models your `commentSchema` can point to.
563564

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+
564612
<h2 id="populate-virtuals"><a href="#populate-virtuals">Populate Virtuals</a></h2>
565613

566614
So far you've only populated based on the `_id` field.

0 commit comments

Comments
 (0)