@@ -1967,9 +1967,13 @@ Query.prototype._optionsForExec = function(model) {
19671967 }
19681968 }
19691969
1970- const projection = this . _fieldsForExec ( ) ;
1971- if ( projection != null ) {
1972- options . projection = projection ;
1970+ this . _applyPaths ( ) ;
1971+ if ( this . _fields != null ) {
1972+ this . _fields = this . _castFields ( this . _fields ) ;
1973+ const projection = this . _fieldsForExec ( ) ;
1974+ if ( projection != null ) {
1975+ options . projection = projection ;
1976+ }
19731977 }
19741978
19751979 return options ;
@@ -2218,10 +2222,6 @@ Query.prototype._find = async function _find() {
22182222 throw this . error ( ) ;
22192223 }
22202224
2221- this . _applyPaths ( ) ;
2222- this . _fields = this . _castFields ( this . _fields ) ;
2223-
2224- const fields = this . _fieldsForExec ( ) ;
22252225 const mongooseOptions = this . _mongooseOptions ;
22262226 const _this = this ;
22272227 const userProvidedFields = _this . _userProvidedFields || { } ;
@@ -2237,8 +2237,8 @@ Query.prototype._find = async function _find() {
22372237 } ) ;
22382238
22392239 const options = this . _optionsForExec ( ) ;
2240- options . projection = this . _fieldsForExec ( ) ;
22412240 const filter = this . _conditions ;
2241+ const fields = options . projection ;
22422242
22432243 const cursor = await this . _collection . collection . find ( filter , options ) ;
22442244 if ( options . explain ) {
@@ -2481,8 +2481,6 @@ Query.prototype._findOne = async function _findOne() {
24812481 throw err ;
24822482 }
24832483
2484- this . _applyPaths ( ) ;
2485- this . _fields = this . _castFields ( this . _fields ) ;
24862484 applyGlobalMaxTimeMS ( this . options , this . model ) ;
24872485 applyGlobalDiskUse ( this . options , this . model ) ;
24882486
@@ -3153,8 +3151,8 @@ function prepareDiscriminatorCriteria(query) {
31533151 * @api public
31543152 */
31553153
3156- Query . prototype . findOneAndUpdate = function ( criteria , doc , options ) {
3157- if ( typeof conditions === 'function' ||
3154+ Query . prototype . findOneAndUpdate = function ( filter , doc , options ) {
3155+ if ( typeof filter === 'function' ||
31583156 typeof doc === 'function' ||
31593157 typeof options === 'function' ||
31603158 typeof arguments [ 3 ] === 'function' ) {
@@ -3170,13 +3168,17 @@ Query.prototype.findOneAndUpdate = function(criteria, doc, options) {
31703168 options = undefined ;
31713169 break ;
31723170 case 1 :
3173- doc = criteria ;
3174- criteria = options = undefined ;
3171+ doc = filter ;
3172+ filter = options = undefined ;
31753173 break ;
31763174 }
31773175
3178- if ( mquery . canMerge ( criteria ) ) {
3179- this . merge ( criteria ) ;
3176+ if ( mquery . canMerge ( filter ) ) {
3177+ this . merge ( filter ) ;
3178+ } else if ( filter != null ) {
3179+ this . error (
3180+ new ObjectParameterError ( filter , 'filter' , 'findOneAndUpdate' )
3181+ ) ;
31803182 }
31813183
31823184 // apply doc
@@ -3378,7 +3380,7 @@ Query.prototype.findOneAndRemove = function(conditions, options) {
33783380 *
33793381 * @method findOneAndDelete
33803382 * @memberOf Query
3381- * @param {Object } [conditions ]
3383+ * @param {Object } [filter ]
33823384 * @param {Object } [options]
33833385 * @param {Boolean } [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html)
33843386 * @param {ClientSession } [options.session=null] The session associated with this query. See [transactions docs](https://mongoosejs.com/docs/transactions.html).
@@ -3388,8 +3390,8 @@ Query.prototype.findOneAndRemove = function(conditions, options) {
33883390 * @api public
33893391 */
33903392
3391- Query . prototype . findOneAndDelete = function ( conditions , options ) {
3392- if ( typeof conditions === 'function' ||
3393+ Query . prototype . findOneAndDelete = function ( filter , options ) {
3394+ if ( typeof filter === 'function' ||
33933395 typeof options === 'function' ||
33943396 typeof arguments [ 2 ] === 'function' ) {
33953397 throw new MongooseError ( 'Query.prototype.findOneAndDelete() no longer accepts a callback' ) ;
@@ -3399,8 +3401,8 @@ Query.prototype.findOneAndDelete = function(conditions, options) {
33993401 this . _validateOp ( ) ;
34003402 this . _validate ( ) ;
34013403
3402- if ( mquery . canMerge ( conditions ) ) {
3403- this . merge ( conditions ) ;
3404+ if ( mquery . canMerge ( filter ) ) {
3405+ this . merge ( filter ) ;
34043406 }
34053407
34063408 options && this . setOptions ( options ) ;
@@ -3512,6 +3514,10 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options) {
35123514
35133515 if ( mquery . canMerge ( filter ) ) {
35143516 this . merge ( filter ) ;
3517+ } else if ( filter != null ) {
3518+ this . error (
3519+ new ObjectParameterError ( filter , 'filter' , 'findOneAndReplace' )
3520+ ) ;
35153521 }
35163522
35173523 if ( replacement != null ) {
@@ -3555,16 +3561,6 @@ Query.prototype._findOneAndReplace = async function _findOneAndReplace() {
35553561 const filter = this . _conditions ;
35563562 const options = this . _optionsForExec ( ) ;
35573563 convertNewToReturnDocument ( options ) ;
3558- let fields = null ;
3559-
3560- this . _applyPaths ( ) ;
3561- if ( this . _fields != null ) {
3562- options . projection = this . _castFields ( clone ( this . _fields ) ) ;
3563- fields = options . projection ;
3564- if ( fields instanceof Error ) {
3565- throw fields ;
3566- }
3567- }
35683564
35693565 const runValidators = _getOption ( this , 'runValidators' , false ) ;
35703566 if ( runValidators === false ) {
@@ -4890,6 +4886,9 @@ Query.prototype._castFields = function _castFields(fields) {
48904886 */
48914887
48924888Query . prototype . _applyPaths = function applyPaths ( ) {
4889+ if ( ! this . model ) {
4890+ return ;
4891+ }
48934892 this . _fields = this . _fields || { } ;
48944893 helpers . applyPaths ( this . _fields , this . model . schema ) ;
48954894
@@ -4948,9 +4947,6 @@ Query.prototype._applyPaths = function applyPaths() {
49484947 */
49494948
49504949Query . prototype . cursor = function cursor ( opts ) {
4951- this . _applyPaths ( ) ;
4952- this . _fields = this . _castFields ( this . _fields ) ;
4953-
49544950 if ( opts ) {
49554951 this . setOptions ( opts ) ;
49564952 }
0 commit comments