Skip to content

Commit 88bb77e

Browse files
mbroadstdaprahamian
authored andcommitted
fix: validate atomic operations in all update methods
We were only validating update operations for atomic operators in the non-pipelined case for `updateMany` and `findOneAndUpdate`. Fixes NODE-1920
1 parent 037d42d commit 88bb77e

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

lib/collection.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,7 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
689689
if (typeof options === 'function') (callback = options), (options = {});
690690
options = options || {};
691691

692-
let err;
693-
if (Array.isArray(update)) {
694-
for (let i = 0; !err && i < update.length; i++) {
695-
err = checkForAtomicOperators(update[i]);
696-
}
697-
} else {
698-
err = checkForAtomicOperators(update);
699-
}
700-
692+
const err = checkForAtomicOperators(update);
701693
if (err) {
702694
if (typeof callback === 'function') return callback(err);
703695
return this.s.promiseLibrary.reject(err);

lib/operations/collection_ops.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,17 @@ function bulkWrite(coll, operations, options, callback) {
156156

157157
// Check the update operation to ensure it has atomic operators.
158158
function checkForAtomicOperators(update) {
159-
const keys = Object.keys(update);
159+
const keys = Array.isArray(update)
160+
? update.reduce((keys, u) => keys.concat(Object.keys(u)), [])
161+
: Object.keys(update);
160162

161163
// same errors as the server would give for update doc lacking atomic operators
162164
if (keys.length === 0) {
163165
return toError('The update operation document must contain at least one atomic operator.');
164166
}
165167

166-
if (keys[0][0] !== '$') {
168+
const foundInvalid = keys.some(key => key[0] !== '$');
169+
if (foundInvalid) {
167170
return toError('the update operation document must contain atomic operators.');
168171
}
169172
}

0 commit comments

Comments
 (0)