Skip to content

Commit

Permalink
docs: add selectPopulatedPaths option
Browse files Browse the repository at this point in the history
Re: #6546
  • Loading branch information
vkarpov15 committed Sep 14, 2018
1 parent 950d223 commit 8a178ad
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/guide.jade
Expand Up @@ -358,6 +358,7 @@ block content
- [collation](#collation)
- [skipVersioning](#skipVersioning)
- [timestamps](#timestamps)
- [selectPopulatedPaths](#selectPopulatedPaths)

<h3 id="autoIndex"><a href="#autoIndex">option: autoIndex</a></h3>

Expand Down Expand Up @@ -867,6 +868,44 @@ block content
});
```

<h3 id="selectPopulatedPaths">
<a href="#selectPopulatedPaths">
option: selectPopulatedPaths
</a>
</h3>

By default, Mongoose will automatically `select()` any populated paths for
you, unless you explicitly exclude them.

```javascript
const bookSchema = new Schema({
title: 'String',
author: { type: 'ObjectId', ref: 'Person' }
});
const Book = mongoose.model('Book', bookSchema);

// By default, Mongoose will add `author` to the below `select()`.
await Book.find().select('title').populate('author');

// In other words, the below query is equivalent to the above
await Book.find().select('title author').populate('author');
```

To opt out of selecting populated fields by default, set `selectPopulatedPaths`
to `false` in your schema.

```javascript
const bookSchema = new Schema({
title: 'String',
author: { type: 'ObjectId', ref: 'Person' }
}, { selectPopulatedPaths: false });
const Book = mongoose.model('Book', bookSchema);

// Because `selectPopulatedPaths` is false, the below doc will **not**
// contain an `author` property.
const doc = await Book.findOne().select('title').populate('author');
```

<h3 id="plugins"><a href="#plugins">Pluggable</a></h3>

Schemas are also [pluggable](./plugins.html) which allows us to package up reusable features into
Expand Down

0 comments on commit 8a178ad

Please sign in to comment.