@@ -2248,6 +2248,9 @@ Query.prototype.error = function error(err) {
22482248 */
22492249
22502250Query . prototype . _unsetCastError = function _unsetCastError ( ) {
2251+ if ( this . _error == null ) {
2252+ return ;
2253+ }
22512254 if ( this . _error != null && ! ( this . _error instanceof CastError ) ) {
22522255 return ;
22532256 }
@@ -2291,9 +2294,9 @@ Query.prototype.mongooseOptions = function(v) {
22912294
22922295Query . prototype . _castConditions = function ( ) {
22932296 let sanitizeFilterOpt = undefined ;
2294- if ( this . model != null && utils . hasUserDefinedProperty ( this . model . db . options , ' sanitizeFilter' ) ) {
2297+ if ( this . model != null && this . model . db . options ?. sanitizeFilter != null ) {
22952298 sanitizeFilterOpt = this . model . db . options . sanitizeFilter ;
2296- } else if ( this . model != null && utils . hasUserDefinedProperty ( this . model . base . options , ' sanitizeFilter' ) ) {
2299+ } else if ( this . model != null && this . model . base . options ?. sanitizeFilter != null ) {
22972300 sanitizeFilterOpt = this . model . base . options . sanitizeFilter ;
22982301 } else {
22992302 sanitizeFilterOpt = this . _mongooseOptions . sanitizeFilter ;
@@ -2536,13 +2539,12 @@ Query.prototype.collation = function(value) {
25362539 * @api private
25372540 */
25382541
2539- Query . prototype . _completeOne = function ( doc , res , callback ) {
2542+ Query . prototype . _completeOne = function ( doc , res , projection , callback ) {
25402543 if ( ! doc && ! this . options . includeResultMetadata ) {
25412544 return callback ( null , null ) ;
25422545 }
25432546
25442547 const model = this . model ;
2545- const projection = clone ( this . _fields ) ;
25462548 const userProvidedFields = this . _userProvidedFields || { } ;
25472549 // `populate`, `lean`
25482550 const mongooseOptions = this . _mongooseOptions ;
@@ -2643,7 +2645,7 @@ Query.prototype._findOne = async function _findOne() {
26432645 // don't pass in the conditions because we already merged them in
26442646 const doc = await this . mongooseCollection . findOne ( this . _conditions , options ) ;
26452647 return new Promise ( ( resolve , reject ) => {
2646- this . _completeOne ( doc , null , ( err , res ) => {
2648+ this . _completeOne ( doc , null , options . projection , ( err , res ) => {
26472649 if ( err ) {
26482650 return reject ( err ) ;
26492651 }
@@ -3238,7 +3240,7 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32383240
32393241 function _init ( err , casted ) {
32403242 if ( err ) {
3241- return immediate ( ( ) => callback ( err ) ) ;
3243+ return callback ( err ) ;
32423244 }
32433245
32443246
@@ -3251,12 +3253,12 @@ function completeOne(model, doc, res, options, fields, userProvidedFields, pop,
32513253 } else {
32523254 res . value = null ;
32533255 }
3254- return immediate ( ( ) => callback ( null , res ) ) ;
3256+ return callback ( null , res ) ;
32553257 }
32563258 if ( options . session != null ) {
32573259 casted . $session ( options . session ) ;
32583260 }
3259- immediate ( ( ) => callback ( null , casted ) ) ;
3261+ callback ( null , casted ) ;
32603262 }
32613263}
32623264
@@ -3465,7 +3467,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
34653467 const doc = ! options . includeResultMetadata ? res : res . value ;
34663468
34673469 return new Promise ( ( resolve , reject ) => {
3468- this . _completeOne ( doc , res , ( err , res ) => {
3470+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
34693471 if ( err ) {
34703472 return reject ( err ) ;
34713473 }
@@ -3561,7 +3563,7 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
35613563 const doc = ! includeResultMetadata ? res : res . value ;
35623564
35633565 return new Promise ( ( resolve , reject ) => {
3564- this . _completeOne ( doc , res , ( err , res ) => {
3566+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
35653567 if ( err ) {
35663568 return reject ( err ) ;
35673569 }
@@ -3715,7 +3717,7 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
37153717
37163718 const doc = ! includeResultMetadata ? res : res . value ;
37173719 return new Promise ( ( resolve , reject ) => {
3718- this . _completeOne ( doc , res , ( err , res ) => {
3720+ this . _completeOne ( doc , res , options . projection , ( err , res ) => {
37193721 if ( err ) {
37203722 return reject ( err ) ;
37213723 }
0 commit comments