Skip to content

Commit

Permalink
fix: findAndModify and options [fields] deprecation
Browse files Browse the repository at this point in the history
`useFindAndModify` default to false now Automattic#6880
and make overwrite option compatibile to fixes Automattic#6887
  • Loading branch information
Fonger committed Aug 18, 2018
1 parent 7875d53 commit ef4a58b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Mongoose.prototype.STATES = STATES;
* Currently supported options are:
* - 'debug': prints the operations mongoose sends to MongoDB to the console
* - 'bufferCommands': enable/disable mongoose's buffering mechanism for all connections and models
* - 'useFindAndModify': true by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
* - 'useFindAndModify': false by default. Set to `true` to make `findOneAndUpdate()` and `findOneAndRemove()` use legacy `findAndModify()` rather than native methods`.
* - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model.
* - 'applyPluginsToDiscriminators': false by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
* - 'objectIdGetter': true by default. Mongoose adds a getter to MongoDB ObjectId's called `_id` that returns `this` for convenience with populate. Set this to false to remove the getter.
Expand Down
6 changes: 3 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1996,8 +1996,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') ||
Expand Down
24 changes: 12 additions & 12 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -2989,7 +2989,7 @@ Query.prototype._findAndModify = function(type, callback) {

var _callback;

var useFindAndModify = true;
var useFindAndModify = false; // default to false now
var runValidators = _getOption(this, 'runValidators', false);
var base = _this.model && _this.model.base;
if ('useFindAndModify' in base.options) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}));
};
Expand All @@ -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);
}));
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ef4a58b

Please sign in to comment.