Skip to content

Commit

Permalink
Merge pull request #13348 from Automattic/vkarpov15/gh-13340
Browse files Browse the repository at this point in the history
fix(query): apply schema-level paths before calculating projection for findOneAndUpdate()
  • Loading branch information
vkarpov15 authored May 2, 2023
2 parents 523f6ce + f2cf85a commit 2bbbb3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
52 changes: 14 additions & 38 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2003,9 +2003,13 @@ Query.prototype._optionsForExec = function(model) {
}
}

const projection = this._fieldsForExec();
if (projection != null) {
options.projection = projection;
this._applyPaths();
if (this._fields != null) {
this._fields = this._castFields(this._fields);
const projection = this._fieldsForExec();
if (projection != null) {
options.projection = projection;
}
}

return options;
Expand Down Expand Up @@ -2258,10 +2262,6 @@ Query.prototype._find = wrapThunk(function(callback) {

callback = _wrapThunkCallback(this, callback);

this._applyPaths();
this._fields = this._castFields(this._fields);

const fields = this._fieldsForExec();
const mongooseOptions = this._mongooseOptions;
const _this = this;
const userProvidedFields = _this._userProvidedFields || {};
Expand All @@ -2276,6 +2276,10 @@ Query.prototype._find = wrapThunk(function(callback) {
lean: mongooseOptions.lean || null
});

const options = this._optionsForExec();
const filter = this._conditions;
const fields = options.projection;

const cb = (err, docs) => {
if (err) {
return callback(err);
Expand Down Expand Up @@ -2317,8 +2321,6 @@ Query.prototype._find = wrapThunk(function(callback) {
});
};

const options = this._optionsForExec();
const filter = this._conditions;

this._collection.collection.find(filter, options, (err, cursor) => {
if (err != null) {
Expand Down Expand Up @@ -2531,8 +2533,6 @@ Query.prototype._findOne = wrapThunk(function(callback) {
return null;
}

this._applyPaths();
this._fields = this._castFields(this._fields);
applyGlobalMaxTimeMS(this.options, this.model);
applyGlobalDiskUse(this.options, this.model);

Expand Down Expand Up @@ -3852,17 +3852,6 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
const filter = this._conditions;
const options = this._optionsForExec();
convertNewToReturnDocument(options);
let fields = null;

this._applyPaths();
if (this._fields != null) {
options.projection = this._castFields(utils.clone(this._fields));
fields = options.projection;
if (fields instanceof Error) {
callback(fields);
return null;
}
}

const runValidators = _getOption(this, 'runValidators', false);
if (runValidators === false) {
Expand Down Expand Up @@ -3991,7 +3980,6 @@ Query.prototype._findAndModify = function(type, callback) {
const model = this.model;
const schema = model.schema;
const _this = this;
let fields;

const castedQuery = castQuery(this);
if (castedQuery instanceof Error) {
Expand Down Expand Up @@ -4062,18 +4050,6 @@ Query.prototype._findAndModify = function(type, callback) {
}
}

this._applyPaths();
if (this._fields) {
this._fields = this._castFields(this._fields);
fields = this._fieldsForExec();
if (fields != null) {
opts.projection = fields;
}
if (opts.projection instanceof Error) {
return callback(opts.projection);
}
}

if (opts.sort) convertSortToArray(opts);

const cb = function(err, doc, res) {
Expand Down Expand Up @@ -5442,6 +5418,9 @@ Query.prototype._castFields = function _castFields(fields) {
*/

Query.prototype._applyPaths = function applyPaths() {
if (!this.model) {
return;
}
this._fields = this._fields || {};
helpers.applyPaths(this._fields, this.model.schema);

Expand Down Expand Up @@ -5500,9 +5479,6 @@ Query.prototype._applyPaths = function applyPaths() {
*/

Query.prototype.cursor = function cursor(opts) {
this._applyPaths();
this._fields = this._castFields(this._fields);

if (opts) {
this.setOptions(opts);
}
Expand Down
1 change: 0 additions & 1 deletion test/model.findOneAndReplace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ describe('model: findOneAndReplace:', function() {
const schema = new Schema({ name: String, age: { type: Number, select: false } });
const Model = db.model('Test', schema);


const doc = await Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
returnOriginal: false
Expand Down
2 changes: 1 addition & 1 deletion test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ describe('Query', function() {
const q = new Query();
q.hint(hint);

const options = q._optionsForExec({ schema: { options: {} } });
const options = q._optionsForExec();
assert.equal(JSON.stringify(options), a);
done();
});
Expand Down

0 comments on commit 2bbbb3c

Please sign in to comment.