Skip to content

Commit

Permalink
fix(query): handle plus path in projection with findOneAndUpdate()
Browse files Browse the repository at this point in the history
Fix #13413
  • Loading branch information
vkarpov15 committed May 24, 2023
1 parent 8263a53 commit 3cf7304
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
26 changes: 6 additions & 20 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3289,19 +3289,15 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
throw this.error();
}

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

const opts = this._optionsForExec(this.model);
this._applyTranslateAliases(opts);

if ('strict' in opts) {
this._mongooseOptions.strict = opts.strict;
if ('strict' in this.options) {
this._mongooseOptions.strict = this.options.strict;
}
const options = { projection: this._fields, ...opts };
const options = this._optionsForExec(this.model);
convertNewToReturnDocument(options);
this._applyTranslateAliases(options);

this._update = this._castUpdate(this._update, false);

Expand All @@ -3312,7 +3308,7 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
this._update, _opts);

if (!this._update || Object.keys(this._update).length === 0) {
if (opts.upsert) {
if (options.upsert) {
// still need to do the upsert to empty doc
const doc = clone(this._update);
delete doc._id;
Expand Down Expand Up @@ -3488,18 +3484,8 @@ Query.prototype._findOneAndDelete = async function _findOneAndDelete() {
}

const filter = this._conditions;
const options = this._optionsForExec();
const options = this._optionsForExec(this.model);
this._applyTranslateAliases(options);
let fields = null;

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

let res = await this._collection.collection.findOneAndDelete(filter, options);
for (const fn of this._transforms) {
Expand Down
28 changes: 28 additions & 0 deletions test/model.findOneAndUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2091,4 +2091,32 @@ describe('model: findOneAndUpdate:', function() {
assert.equal(err.name, 'ObjectParameterError');
});

it('handles plus path in projection (gh-13413)', async function() {
const testSchema = new mongoose.Schema({
name: String,
nickName: {
type: String,
select: false
}
});
const Test = db.model('Test', testSchema);

const entry = await Test.create({
name: 'Test Testerson',
nickName: 'Quiz'
});

let res = await Test.findOneAndUpdate(
{ _id: entry._id },
{ $set: { name: 'Test' } },
{ projection: '+nickName', returnDocument: 'after' }
);
assert.equal(res.nickName, 'Quiz');

res = await Test.findOneAndDelete(
{ _id: entry._id },
{ projection: '+nickName', returnDocument: 'before' }
);
assert.equal(res.nickName, 'Quiz');
});
});

0 comments on commit 3cf7304

Please sign in to comment.