Skip to content

Commit

Permalink
fix(query): apply schema-level projections to the result of `findOneA…
Browse files Browse the repository at this point in the history
…ndReplace()`

Fix #7654
  • Loading branch information
vkarpov15 committed May 27, 2019
1 parent 85efe9b commit f94cc4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3216,6 +3216,7 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {

const castedDoc = new this.model(this._update, null, true);

this._applyPaths();
if (this._fields != null) {
options.projection = this._castFields(utils.clone(this._fields));
fields = options.projection;
Expand Down
14 changes: 14 additions & 0 deletions test/model.findOneAndReplace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,18 @@ describe('model: findOneAndReplace:', function() {
err.errors['age'].message);
});
});

it('schema-level projection (gh-7654)', function() {
const schema = new Schema({ name: String, age: { type: Number, select: false } });
const Model = db.model('gh7654_0', schema);

return co(function*() {
const doc = yield Model.findOneAndReplace({}, { name: 'Jean-Luc Picard', age: 59 }, {
upsert: true,
returnOriginal: false
});

assert.ok(!doc.age);
});
});
});

0 comments on commit f94cc4e

Please sign in to comment.