Skip to content

Commit

Permalink
fix(collection): allow { upsert: 1 } for findOneAndUpdate() and updat…
Browse files Browse the repository at this point in the history
…e() (#1580)

Re: Automattic/mongoose#5839

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.
  • Loading branch information
vkarpov15 authored and daprahamian committed Dec 4, 2017
1 parent f55c9c5 commit 0f338c8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,8 +1044,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;

// Have we specified collation
decorateWithCollation(finalOptions, self, options);
Expand Down Expand Up @@ -2371,8 +2371,8 @@ var findOneAndUpdate = function(self, filter, update, 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(
Expand Down

0 comments on commit 0f338c8

Please sign in to comment.