Skip to content

Commit ce4487c

Browse files
authored
Update populate.md
1 parent 12bdc23 commit ce4487c

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

docs/populate.md

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -554,67 +554,43 @@ comments[0].product.name; // "The Count of Monte Cristo"
554554
comments[1].blogPost.title; // "Top 10 French Novels"
555555
```
556556

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.
559566

560567
```javascript
561568
const commentSchema = new Schema({
562569
body: { type: String, required: true },
563-
verifiedBuyer: Boolean,
564-
doc: {
570+
commentType: {
571+
type: String,
572+
enum: ['comment', 'review']
573+
},
574+
entityId: {
565575
type: Schema.Types.ObjectId,
566576
required: true,
567577
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
569579
}
570580
},
571-
docModel: {
581+
commentEntityModel: {
572582
type: String,
573583
required: true,
574584
enum: ['BlogPost', 'Review']
575585
},
576-
otherOption: {
586+
reviewEntityModel: {
577587
type: String,
578588
required: true,
579589
enum: ['Vendor', 'Product']
580590
}
581591
});
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-
608592
```
609593

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-
618594
<h2 id="dynamic-ref"><a href="#dynamic-ref">Dynamic References via <code>ref</code></a></h2>
619595

620596
Just like `refPath`, `ref` can also be assigned a function.
@@ -631,10 +607,6 @@ const commentSchema = new Schema({
631607
}
632608
},
633609
});
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);
638610
```
639611

640612
<h2 id="populate-virtuals"><a href="#populate-virtuals">Populate Virtuals</a></h2>

0 commit comments

Comments
 (0)