diff --git a/lib/collection.js b/lib/collection.js index 9343c7f74d9..9a692225e2b 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -689,15 +689,7 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; - let err; - if (Array.isArray(update)) { - for (let i = 0; !err && i < update.length; i++) { - err = checkForAtomicOperators(update[i]); - } - } else { - err = checkForAtomicOperators(update); - } - + const err = checkForAtomicOperators(update); if (err) { if (typeof callback === 'function') return callback(err); return this.s.promiseLibrary.reject(err); diff --git a/lib/operations/collection_ops.js b/lib/operations/collection_ops.js index 6ab8d4435be..0339eda4baf 100644 --- a/lib/operations/collection_ops.js +++ b/lib/operations/collection_ops.js @@ -156,14 +156,17 @@ function bulkWrite(coll, operations, options, callback) { // Check the update operation to ensure it has atomic operators. function checkForAtomicOperators(update) { - const keys = Object.keys(update); + const keys = Array.isArray(update) + ? update.reduce((keys, u) => keys.concat(Object.keys(u)), []) + : 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] !== '$') { + const foundInvalid = keys.some(key => key[0] !== '$'); + if (foundInvalid) { return toError('the update operation document must contain atomic operators.'); } }