Skip to content

Commit

Permalink
If a query has only real fields selected, no virtual fields will be i…
Browse files Browse the repository at this point in the history
…ncluded in the query
  • Loading branch information
makinde committed Oct 23, 2018
1 parent 688abe6 commit f17ca99
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Allows you to specify which virtuals fields should be returned in find queries w
* Queries can be set as lean using any style, e.g. `myQuery.lean()`, `myQuery.setOptions({ lean: true })`, `myQuery.lean({virtuals: true})`, etc.
* If only virtual fields are present in the `select` statement, no real properties will be retrieved from the database.
* Specifying a virtual field in the `select` statement will exclude non-specified virtual fields, even if lean is set to `{virtuals: true}` (which would normally trigger all virtuals to be included). `Model.find({}, 'virtual_key').lean()` is the same as `.lean({ virtuals: ['virtual_key']})`.
* If a query has only real fields selected, no virtual fields will be included in the query

## Getting Started

Expand Down Expand Up @@ -55,8 +56,6 @@ We use [SemVer](http://semver.org/) for versioning and [np](https://www.npmjs.co

* **Makinde Adeagbo** - [makinde (github)](https://github.com/makinde)

See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.

## License

This project is licensed under the ISC License - see the [LICENSE.md](LICENSE.md) file for details
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ module.exports = function selectVirtuals(schema) {

// At this point there was at least one virtual field mentioned in the selection. If
// there are no more fields left in the selection, it means only virtual fields
// were selected and we shouldn't return any real fields. We need to explcitly set
// were selected and we shouldn't return any real fields. We need to explicitly set
// only the `_id` field to come back since leaving the selection object blank would
// return all real fields.
if (Object.keys(selection).length === 0) {
selection['_id'] = 1;
}
} else if (Object.keys(selection).length > 0) {
// There are real fields specified, but no virtual keys specified
// In this case, don't return any virtuals
this._mongooseOptions.lean = true;

This comment has been minimized.

Copy link
@pepkin88

pepkin88 Jun 5, 2023

Potential problem when working with other Mongoose plugins that use the lean option to pass settings, like "mongoose-lean-defaults" or "mongoose-lean-getters".
this._mongooseOptions.lean should not be replaced with true, because it means clearing settings for those other plugins, e.g. {virtuals: true, getters: true} would be overwritten by true.

}

next();
Expand Down
7 changes: 7 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ test('No fields selected on lean query', async t => {
t.is(result.virtual_key2, undefined, 'No virtuals should be returned');
});

test('Only a real key in selection w/ lean virtuals', async t=> {
const result = await Model.findOne({}, 'real_key').lean({virtuals: true}).exec();
t.is(result.real_key, 'foo');
t.is(result.virtual_key1, undefined, 'No virtuals should be returned');
t.is(result.virtual_key2, undefined, 'No virtuals should be returned');
});

test.after.always(async () => {
await Model.remove({});
await mongoose.disconnect();
Expand Down

0 comments on commit f17ca99

Please sign in to comment.