-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(updateOne/updateMany): ensure that update documents contain atomi…
…c operators NODE-965
- Loading branch information
Showing
6 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -938,7 +938,15 @@ define.classMethod('insert', { callback: true, promise: true }); | |
*/ | ||
Collection.prototype.updateOne = function(filter, update, options, callback) { | ||
var self = this; | ||
|
||
if (typeof options === 'function') (callback = options), (options = {}); | ||
|
||
var err = checkForAtomicOperators(update); | ||
if (err) { | ||
if (typeof callback === 'function') return callback(err); | ||
return this.s.promiseLibrary.reject(err); | ||
} | ||
|
||
options = shallowClone(options); | ||
|
||
// Add ignoreUndfined | ||
|
@@ -959,6 +967,19 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { | |
}); | ||
}; | ||
|
||
var checkForAtomicOperators = function(update) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mbroadst
Member
|
||
var keys = Object.keys(update); | ||
|
||
// same errors as the server would give for update doc lacking atomic operators | ||
if (keys.length === 0) { | ||
return toError('The update operation document must contain at least one atomic operator.'); | ||
} | ||
|
||
if (keys[0][0] !== '$') { | ||
return toError('the update operation document must contain atomic operators.'); | ||
} | ||
}; | ||
|
||
var updateOne = function(self, filter, update, options, callback) { | ||
// Set single document update | ||
options.multi = false; | ||
|
@@ -1061,6 +1082,13 @@ define.classMethod('replaceOne', { callback: true, promise: true }); | |
Collection.prototype.updateMany = function(filter, update, options, callback) { | ||
var self = this; | ||
if (typeof options === 'function') (callback = options), (options = {}); | ||
|
||
var err = checkForAtomicOperators(update); | ||
if (err) { | ||
if (typeof callback === 'function') return callback(err); | ||
return this.s.promiseLibrary.reject(err); | ||
} | ||
|
||
options = shallowClone(options); | ||
|
||
// Add ignoreUndfined | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Does the updateOne method no longer support passing in a document as
update
? That won't have a "$" and implicitly an "atomic" operator?