Skip to content

Commit

Permalink
docs(populate): add note about setting toObject for populate virtuals
Browse files Browse the repository at this point in the history
Fix #9822
  • Loading branch information
vkarpov15 committed Jan 18, 2021
1 parent f730aff commit 6b65d0d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions docs/populate.pug
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,16 @@ block content
});
```

Keep in mind that virtuals are _not_ included in `toJSON()` output by default. If you want populate virtuals to show up when using functions
that rely on `JSON.stringify()`, like Express'
[`res.json()` function](http://expressjs.com/en/4x/api.html#res.json),
Keep in mind that virtuals are _not_ included in `toJSON()` and `toObject()` output by default. If you want populate
virtuals to show up when using functions that rely on `JSON.stringify()`, like Express'
[`res.json()` function](https://masteringjs.io/tutorials/express/json),
set the `virtuals: true` option on your schema's `toJSON` options.

```javascript
// Set `virtuals: true` so `res.json()` works
const BandSchema = new Schema({
name: String
}, { toJSON: { virtuals: true } });
const BandSchema = new Schema({ name: String }, {
toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
toObject: { virtuals: true } // So `toObject()` output includes virtuals
});
```

If you're using populate projections, make sure `foreignField` is included
Expand Down

0 comments on commit 6b65d0d

Please sign in to comment.