@@ -2003,9 +2003,13 @@ Query.prototype._optionsForExec = function(model) {
2003
2003
}
2004
2004
}
2005
2005
2006
- const projection = this . _fieldsForExec ( ) ;
2007
- if ( projection != null ) {
2008
- options . projection = projection ;
2006
+ this . _applyPaths ( ) ;
2007
+ if ( this . _fields != null ) {
2008
+ this . _fields = this . _castFields ( this . _fields ) ;
2009
+ const projection = this . _fieldsForExec ( ) ;
2010
+ if ( projection != null ) {
2011
+ options . projection = projection ;
2012
+ }
2009
2013
}
2010
2014
2011
2015
return options ;
@@ -2258,10 +2262,6 @@ Query.prototype._find = wrapThunk(function(callback) {
2258
2262
2259
2263
callback = _wrapThunkCallback ( this , callback ) ;
2260
2264
2261
- this . _applyPaths ( ) ;
2262
- this . _fields = this . _castFields ( this . _fields ) ;
2263
-
2264
- const fields = this . _fieldsForExec ( ) ;
2265
2265
const mongooseOptions = this . _mongooseOptions ;
2266
2266
const _this = this ;
2267
2267
const userProvidedFields = _this . _userProvidedFields || { } ;
@@ -2276,6 +2276,10 @@ Query.prototype._find = wrapThunk(function(callback) {
2276
2276
lean : mongooseOptions . lean || null
2277
2277
} ) ;
2278
2278
2279
+ const options = this . _optionsForExec ( ) ;
2280
+ const filter = this . _conditions ;
2281
+ const fields = options . projection ;
2282
+
2279
2283
const cb = ( err , docs ) => {
2280
2284
if ( err ) {
2281
2285
return callback ( err ) ;
@@ -2317,8 +2321,6 @@ Query.prototype._find = wrapThunk(function(callback) {
2317
2321
} ) ;
2318
2322
} ;
2319
2323
2320
- const options = this . _optionsForExec ( ) ;
2321
- const filter = this . _conditions ;
2322
2324
2323
2325
this . _collection . collection . find ( filter , options , ( err , cursor ) => {
2324
2326
if ( err != null ) {
@@ -2531,8 +2533,6 @@ Query.prototype._findOne = wrapThunk(function(callback) {
2531
2533
return null ;
2532
2534
}
2533
2535
2534
- this . _applyPaths ( ) ;
2535
- this . _fields = this . _castFields ( this . _fields ) ;
2536
2536
applyGlobalMaxTimeMS ( this . options , this . model ) ;
2537
2537
applyGlobalDiskUse ( this . options , this . model ) ;
2538
2538
@@ -3852,17 +3852,6 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
3852
3852
const filter = this . _conditions ;
3853
3853
const options = this . _optionsForExec ( ) ;
3854
3854
convertNewToReturnDocument ( options ) ;
3855
- let fields = null ;
3856
-
3857
- this . _applyPaths ( ) ;
3858
- if ( this . _fields != null ) {
3859
- options . projection = this . _castFields ( utils . clone ( this . _fields ) ) ;
3860
- fields = options . projection ;
3861
- if ( fields instanceof Error ) {
3862
- callback ( fields ) ;
3863
- return null ;
3864
- }
3865
- }
3866
3855
3867
3856
const runValidators = _getOption ( this , 'runValidators' , false ) ;
3868
3857
if ( runValidators === false ) {
@@ -3991,7 +3980,6 @@ Query.prototype._findAndModify = function(type, callback) {
3991
3980
const model = this . model ;
3992
3981
const schema = model . schema ;
3993
3982
const _this = this ;
3994
- let fields ;
3995
3983
3996
3984
const castedQuery = castQuery ( this ) ;
3997
3985
if ( castedQuery instanceof Error ) {
@@ -4062,18 +4050,6 @@ Query.prototype._findAndModify = function(type, callback) {
4062
4050
}
4063
4051
}
4064
4052
4065
- this . _applyPaths ( ) ;
4066
- if ( this . _fields ) {
4067
- this . _fields = this . _castFields ( this . _fields ) ;
4068
- fields = this . _fieldsForExec ( ) ;
4069
- if ( fields != null ) {
4070
- opts . projection = fields ;
4071
- }
4072
- if ( opts . projection instanceof Error ) {
4073
- return callback ( opts . projection ) ;
4074
- }
4075
- }
4076
-
4077
4053
if ( opts . sort ) convertSortToArray ( opts ) ;
4078
4054
4079
4055
const cb = function ( err , doc , res ) {
@@ -5442,6 +5418,9 @@ Query.prototype._castFields = function _castFields(fields) {
5442
5418
*/
5443
5419
5444
5420
Query . prototype . _applyPaths = function applyPaths ( ) {
5421
+ if ( ! this . model ) {
5422
+ return ;
5423
+ }
5445
5424
this . _fields = this . _fields || { } ;
5446
5425
helpers . applyPaths ( this . _fields , this . model . schema ) ;
5447
5426
@@ -5500,9 +5479,6 @@ Query.prototype._applyPaths = function applyPaths() {
5500
5479
*/
5501
5480
5502
5481
Query . prototype . cursor = function cursor ( opts ) {
5503
- this . _applyPaths ( ) ;
5504
- this . _fields = this . _castFields ( this . _fields ) ;
5505
-
5506
5482
if ( opts ) {
5507
5483
this . setOptions ( opts ) ;
5508
5484
}
0 commit comments