Skip to content

Commit

Permalink
docs(populate): make path names in refPath section consistent
Browse files Browse the repository at this point in the history
Fix #11724
  • Loading branch information
vkarpov15 committed Jun 11, 2022
1 parent f533966 commit 53d2625
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions docs/populate.md
Expand Up @@ -493,14 +493,14 @@ storing comments. A user may comment on either a blog post or a product.
```javascript
const commentSchema = new Schema({
body: { type: String, required: true },
modelId: {
doc: {
type: Schema.Types.ObjectId,
required: true,
// Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
// will look at the `onModel` property to find the right model.
refPath: 'onModel'
refPath: 'docModel'
},
onModel: {
docModel: {
type: String,
required: true,
enum: ['BlogPost', 'Product']
Expand All @@ -512,10 +512,9 @@ const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));
const Comment = mongoose.model('Comment', commentSchema);
```

The `refPath` option is a more sophisticated alternative to `ref`. If `ref`
is just a string, Mongoose will always query the same model to find the
populated subdocs. With `refPath`, you can configure what model Mongoose
uses for each document.
The `refPath` option is a more sophisticated alternative to `ref`.
If `ref` is a string, Mongoose will always query the same model to find the populated subdocs.
With `refPath`, you can configure what model Mongoose uses for each document.

```javascript
const book = await Product.create({ name: 'The Count of Monte Cristo' });
Expand All @@ -540,9 +539,7 @@ comments[0].doc.name; // "The Count of Monte Cristo"
comments[1].doc.title; // "Top 10 French Novels"
```

An alternative approach is to define separate `blogPost` and
`product` properties on `commentSchema`, and then `populate()` on both
properties.
An alternative approach is to define separate `blogPost` and `product` properties on `commentSchema`, and then `populate()` on both properties.

```javascript
const commentSchema = new Schema({
Expand Down

0 comments on commit 53d2625

Please sign in to comment.