Skip to content

Commit

Permalink
fix(collection): allow { upsert: 1 } for findOneAndUpdate() and update()
Browse files Browse the repository at this point in the history
This is a very rough edge in the API where findAndModify() treats upsert: 1
as upsert: true, but findOneAndUpdate() and updateX() treat upsert: 1
as upsert: false. CRUD spec does say upsert is a boolean but we've had
upsert: 1 in shell examples for a while so it may be worthwhile to support
both, especially since truthiness is so common in JS.

Port of #1580 to 3.0.0
  • Loading branch information
daprahamian authored and mbroadst committed Dec 4, 2017
1 parent 4a0cfeb commit 5bcedd6
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,8 +1014,8 @@ var updateDocuments = function(self, selector, document, options, callback) {

// Execute the operation
var op = { q: selector, u: document };
op.upsert = typeof options.upsert === 'boolean' ? options.upsert : false;
op.multi = typeof options.multi === 'boolean' ? options.multi : false;
op.upsert = options.upsert !== void 0 ? !!options.upsert : false;
op.multi = options.multi !== void 0 ? !!options.multi : false;

if (finalOptions.arrayFilters) {
op.arrayFilters = finalOptions.arrayFilters;
Expand Down Expand Up @@ -2124,9 +2124,8 @@ var findOneAndReplace = function(self, filter, replacement, options, callback) {
var finalOptions = shallowClone(options);
finalOptions['fields'] = options.projection;
finalOptions['update'] = true;
finalOptions['new'] =
typeof options.returnOriginal === 'boolean' ? !options.returnOriginal : false;
finalOptions['upsert'] = typeof options.upsert === 'boolean' ? options.upsert : false;
finalOptions['new'] = options.returnOriginal !== void 0 ? !options.returnOriginal : false;
finalOptions['upsert'] = options.upsert !== void 0 ? !!options.upsert : false;

// Execute findAndModify
self.findAndModify(filter, options.sort, replacement, finalOptions, callback);
Expand Down

0 comments on commit 5bcedd6

Please sign in to comment.