Permalink
Browse files

Merge pull request #6888 from Fonger/deprecation-fix

WIP: fix: findAndModify and options [fields] deprecation
  • Loading branch information...
vkarpov15 committed Aug 20, 2018
2 parents 81741dd + d0d9f09 commit 3b57acccf4b7f71a87e684790014ba19a02d3555
Showing with 57 additions and 14 deletions.
  1. +3 −3 lib/model.js
  2. +11 −11 lib/query.js
  3. +43 −0 test/model.findOneAndUpdate.test.js
View
@@ -836,7 +836,7 @@ Model.prototype.$__remove = function $__remove(options, cb) {
return cb(where);
}
this.collection.remove(where, options, err => {
this.collection.deleteOne(where, options, err => {
if (!err) {
this.$__.isDeleted = true;
this.emit('remove', this);
@@ -1998,8 +1998,8 @@ Model.findOneAndUpdate = function(conditions, update, options, callback) {
}
var fields;
if (options && options.fields) {
fields = options.fields;
if (options) {
fields = options.fields || options.projection;
}
var retainKeyOrder = get(options, 'retainKeyOrder') ||
View
@@ -1684,7 +1684,7 @@ Query.prototype._find = function(callback) {
};
var options = this._optionsForExec();
options.fields = this._fieldsForExec();
options.projection = this._fieldsForExec();
var filter = this._conditions;
this._collection.find(filter, options, cb);
return null;
@@ -2971,9 +2971,9 @@ Query.prototype._findAndModify = function(type, callback) {
if (this._fields) {
fields = utils.clone(this._fields);
opts.fields = this._castFields(fields);
if (opts.fields instanceof Error) {
return callback(opts.fields);
opts.projection = this._castFields(fields);
if (opts.projection instanceof Error) {
return callback(opts.projection);
}
}
@@ -3005,10 +3005,6 @@ Query.prototype._findAndModify = function(type, callback) {
opts.returnOriginal = !opts['new'];
delete opts['new'];
}
if ('fields' in opts) {
opts.projection = opts.fields;
delete opts.fields;
}
if (type === 'remove') {
collection.findOneAndDelete(castedQuery, opts, utils.tick(function(error, res) {
@@ -3018,6 +3014,9 @@ Query.prototype._findAndModify = function(type, callback) {
return this;
}
// honors legacy overwrite option for backward compatibility
const updateMethod = isOverwriting ? 'findOneAndReplace' : 'findOneAndUpdate';
if (runValidators && doValidate) {
_callback = function(error) {
if (error) {
@@ -3026,7 +3025,8 @@ Query.prototype._findAndModify = function(type, callback) {
if (castedDoc && castedDoc.toBSON) {
castedDoc = castedDoc.toBSON();
}
collection.findOneAndUpdate(castedQuery, castedDoc, opts, utils.tick(function(error, res) {
collection[updateMethod](castedQuery, castedDoc, opts, utils.tick(function(error, res) {
return cb(error, res ? res.value : res, res);
}));
};
@@ -3040,7 +3040,7 @@ Query.prototype._findAndModify = function(type, callback) {
if (castedDoc && castedDoc.toBSON) {
castedDoc = castedDoc.toBSON();
}
collection.findOneAndUpdate(castedQuery, castedDoc, opts, utils.tick(function(error, res) {
collection[updateMethod](castedQuery, castedDoc, opts, utils.tick(function(error, res) {
return cb(error, res ? res.value : res, res);
}));
}
@@ -3953,7 +3953,7 @@ Query.prototype._applyPaths = function applyPaths() {
Query.prototype.cursor = function cursor(opts) {
this._applyPaths();
this._fields = this._castFields(this._fields);
this.setOptions({ fields: this._fieldsForExec() });
this.setOptions({ projection: this._fieldsForExec() });
if (opts) {
this.setOptions(opts);
}
@@ -1932,6 +1932,49 @@ describe('model: findOneAndUpdate:', function() {
});
});
it('useFindAndModify with overwrite (gh-6887)', function() {
return co(function*() {
const m = new mongoose.constructor();
yield m.connect(start.uri);
const calls = [];
m.set('debug', function(collection, fnName) {
calls.push({ collection: collection, fnName: fnName });
});
m.set('useFindAndModify', false);
const schema = new m.Schema({
name: String,
age: Number,
location: String
});
const Model = m.model('gh6887', schema);
const options = { overwrite: true, new: true };
const doc = yield Model.create({ name: 'Jennifer', location: 'Taipei' });
const newDoc1 = yield Model.findOneAndUpdate({ name: 'Jennifer' }, { age: 24 }, options);
const newDoc2 = yield Model.findByIdAndUpdate(doc._id, { name: 'Fonger', location: 'Hsinchu' }, options);
assert.strictEqual(newDoc1.name, undefined);
assert.strictEqual(newDoc1.age, 24);
assert.strictEqual(newDoc1.location, undefined);
assert.strictEqual(newDoc2.name, 'Fonger');
assert.strictEqual(newDoc2.age, undefined);
assert.strictEqual(newDoc2.location, 'Hsinchu');
assert.equal(calls.length, 3);
assert.equal(calls[1].collection, 'gh6887');
assert.equal(calls[1].collection, 'gh6887');
assert.equal(calls[2].fnName, 'findOneAndReplace');
assert.equal(calls[2].fnName, 'findOneAndReplace');
m.disconnect();
});
});
it('update validators with pull + $in (gh-6240)', function() {
const highlightSchema = new mongoose.Schema({
_id: {

0 comments on commit 3b57acc

Please sign in to comment.