From dc1387e9aa3aa64425f440595ff40879e47214a0 Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Wed, 12 Jun 2019 13:07:07 -0400 Subject: [PATCH] feat(Update): add the ability to specify a pipeline to an update command (#2017) * feat(Update): add the ability to specify a pipeline to an update command NODE-1920 --- lib/collection.js | 10 ++++++++- test/functional/collection_tests.js | 32 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/collection.js b/lib/collection.js index af20742074..16466422c5 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -686,7 +686,15 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; - const err = checkForAtomicOperators(update); + let err; + if (Array.isArray(update)) { + for (let i = 0; !err && i < update.length; i++) { + err = checkForAtomicOperators(update[i]); + } + } else { + err = checkForAtomicOperators(update); + } + if (err) { if (typeof callback === 'function') return callback(err); return this.s.promiseLibrary.reject(err); diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index 58c3bbf6dc..2f6871ea64 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1918,4 +1918,36 @@ describe('Collection', function() { client.close(); }); }); + + it('should correctly update with pipeline', { + metadata: { + requires: { mongodb: '>=4.2.0' } + }, + + // The actual test we wish to run + test: function(done) { + const configuration = this.configuration; + const client = configuration.newClient(configuration.writeConcernMax(), { + poolSize: 1 + }); + + client.connect((err, client) => { + const db = client.db(configuration.db); + + db.createCollection('test_should_correctly_do_update_with_pipeline', (err, collection) => { + collection.updateOne( + {}, + [{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }], + configuration.writeConcernMax(), + (err, r) => { + expect(err).to.not.exist; + expect(r.result.n).to.equal(0); + + client.close(done); + } + ); + }); + }); + } + }); });