The easiest way is to use the patch mongoose.Query class for the monkey to add a default default option:
var __setOptions = mongoose.Query.prototype.setOptions; mongoose.Query.prototype.setOptions = function(options, overwrite) { __setOptions.apply(this, arguments); if (this.options.lean == null) this.options.lean = true; return this; };
Mongoose creates a new mongoose.Query instance for each query, and the call to setOptions is part of the mongoose.Query construct.
By mongoose.Query class, you can turn the worst queries globally. Thus, you do not need to use all the mongoose methods ( find , findOne , findById , findOneAndUpdate , etc.).
mongoose uses the Query class for internal calls, such as padding. It passes the original Query parameters for each subquery, so there should be no problems, but be careful with this solution.
Leonid Beschastny
source share