Skip to content

Commit

Permalink
fix(populate): add selectPopulatedPaths option to opt out of auto-add…
Browse files Browse the repository at this point in the history
…ing `populate()`-ed fields to `select()`

Fix #6546
  • Loading branch information
vkarpov15 committed Sep 14, 2018
1 parent c0fb363 commit 950d223
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -93,6 +93,7 @@ Mongoose.prototype.STATES = STATES;
* - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
* - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
* - 'runValidators': false by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default.
* - 'selectPopulatedPaths': true by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
*
* @param {String} key
* @param {String|Function|Boolean} value
Expand Down
14 changes: 13 additions & 1 deletion lib/query.js
Expand Up @@ -3931,7 +3931,19 @@ Query.prototype._castFields = function _castFields(fields) {
Query.prototype._applyPaths = function applyPaths() {
this._fields = this._fields || {};
helpers.applyPaths(this._fields, this.model.schema);
selectPopulatedFields(this);

let _selectPopulatedPaths = true;

if ('selectPopulatedPaths' in this.model.base.options) {
_selectPopulatedPaths = this.model.base.options.selectPopulatedPaths;
}
if ('selectPopulatedPaths' in this.model.schema.options) {
_selectPopulatedPaths = this.model.schema.options.selectPopulatedPaths;
}

if (_selectPopulatedPaths) {
selectPopulatedFields(this);
}
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/schema.js
Expand Up @@ -49,6 +49,7 @@ let MongooseTypes;
* - [validateBeforeSave](/docs/guide.html#validateBeforeSave) - bool - defaults to `true`
* - [versionKey](/docs/guide.html#versionKey): string - defaults to "__v"
* - [collation](/docs/guide.html#collation): object - defaults to null (which means use no collation)
* - [selectPopulatedPaths](/docs/guide.html#selectPopulatedPaths): boolean - defaults to `true`
*
* ####Note:
*
Expand Down

0 comments on commit 950d223

Please sign in to comment.