Skip to content

Commit 0f338c8

Browse files
vkarpov15daprahamian
authored andcommitted
fix(collection): allow { upsert: 1 } for findOneAndUpdate() and update() (#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.
1 parent f55c9c5 commit 0f338c8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/collection.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ var updateDocuments = function(self, selector, document, options, callback) {
10441044

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

10501050
// Have we specified collation
10511051
decorateWithCollation(finalOptions, self, options);
@@ -2371,8 +2371,8 @@ var findOneAndUpdate = function(self, filter, update, options, callback) {
23712371
var finalOptions = shallowClone(options);
23722372
finalOptions['fields'] = options.projection;
23732373
finalOptions['update'] = true;
2374-
finalOptions['new'] = typeof options.returnOriginal == 'boolean' ? !options.returnOriginal : false;
2375-
finalOptions['upsert'] = typeof options.upsert == 'boolean' ? options.upsert : false;
2374+
finalOptions['new'] = options.returnOriginal !== void 0 ? !options.returnOriginal : false;
2375+
finalOptions['upsert'] = options.upsert !== void 0 ? !!options.upsert : false;
23762376

23772377
// Execute findAndModify
23782378
self.findAndModify(

0 commit comments

Comments
 (0)