Mongoose: Add ability to check whether `lean()` is enabled on a query for middleware. Introduce `Query#mongooseOptions()`

Created on 1 Nov 2019  路  2Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
feature

What is the current behavior?
I can't set lean: true as the default option globally or per schema/model

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
I'd like to set lean: true as the default behavior globally, and be able to lean: false on specific calls of findOne and related methods

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node: 12.13.0
mongoose: 5.7.7
mongo: 4.2

related to https://github.com/Automattic/mongoose/issues/6266

Most helpful comment

mongoose.plugin(schema => {
  schema.pre(['find', 'findOne'], function() {
    this.lean();
  });
});

That should work for setting all find() and findOne() queries to lean. The downside is that right now there's no way to tell whether a query has lean() set or not without using private state. We'll add that for 5.8.0.

All 2 comments

lean: false is very slow when your collection have a very deep and nested structure

mongoose.plugin(schema => {
  schema.pre(['find', 'findOne'], function() {
    this.lean();
  });
});

That should work for setting all find() and findOne() queries to lean. The downside is that right now there's no way to tell whether a query has lean() set or not without using private state. We'll add that for 5.8.0.

Was this page helpful?
0 / 5 - 0 ratings