Skip to content

Commit b60452f

Browse files
committed
Update populate.md
1 parent b7d46a0 commit b60452f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

docs/populate.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,37 +560,49 @@ selects a refpath depending on a value on the document being populated. For exam
560560
```javascript
561561
const commentSchema = new Schema({
562562
body: { type: String, required: true },
563+
verifiedBuyer: Boolean,
563564
doc: {
564565
type: Schema.Types.ObjectId,
565566
required: true,
566567
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
568569
}
569570
},
570571
docModel: {
571572
type: String,
572573
required: true,
573-
enum: ['BlogPost', 'Product']
574+
enum: ['BlogPost', 'Review']
575+
},
576+
otherOption: {
577+
type: String,
578+
required: true,
579+
enum: ['Vendor', 'Product']
574580
}
575581
});
576582

577583
const Product = mongoose.model('Product', new Schema({ name: String }));
578584
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 }));
579587
const Comment = mongoose.model('Comment', commentSchema);
580588

581589
const book = await Product.create({ name: 'The Count of Monte Cristo' });
582590
const post = await BlogPost.create({ title: 'Top 10 French Novels' });
583591

584592
const commentOnBook = await Comment.create({
585593
body: 'Great read',
594+
verifiedBuyer: true
586595
doc: book._id,
587-
docModel: 'Product'
596+
docModel: 'Review',
597+
otherOption: 'Product'
588598
});
589599

590600
const commentOnPost = await Comment.create({
591601
body: 'Very informative',
602+
verifiedBuyer: false,
592603
doc: post._id,
593-
docModel: 'BlogPost'
604+
docModel: 'BlogPost',
605+
otherOption: 'Vendor'
594606
});
595607

596608
```

0 commit comments

Comments
 (0)