From 484be5f37edc9c074a9dc266ded1894cd1c415f4 Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Mon, 10 Jun 2019 13:16:05 -0400 Subject: [PATCH 1/5] feat(Update): add the ability to specify a pipeline to an update command NODE-1920 --- lib/collection.js | 5 ++++- test/functional/collection_tests.js | 32 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/collection.js b/lib/collection.js index 067cb4dbaf..4168205ed4 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -686,7 +686,10 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; - const err = checkForAtomicOperators(update); + const err = Array.isArray(update) + ? update.forEach(checkForAtomicOperators) + : 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 dd06ac44e6..25450b19fc 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1898,4 +1898,36 @@ describe('Collection', function() { } }); }); + + it('should correctly update with array of docs', { + metadata: { + requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + }, + + // 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_docs_array', (err, collection) => { + collection.updateOne( + {}, + [{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }], + configuration.writeConcernMax(), + (err, r) => { + expect(err).to.equal(null); + expect(r.result.n).to.equal(0); + + client.close(done); + } + ); + }); + }); + } + }); }); From c80798fff993bf4e16ee7cb9b776f72091df605e Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Mon, 10 Jun 2019 13:44:42 -0400 Subject: [PATCH 2/5] fix linter errors --- test/functional/collection_tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index 25450b19fc..505c58aa1b 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1899,7 +1899,7 @@ describe('Collection', function() { }); }); - it('should correctly update with array of docs', { + it('should correctly update with pipeline', { metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } }, @@ -1914,7 +1914,7 @@ describe('Collection', function() { client.connect((err, client) => { const db = client.db(configuration.db); - db.createCollection('test_should_correctly_do_update_with_docs_array', (err, collection) => { + db.createCollection('test_should_correctly_do_update_with_pipeline', (err, collection) => { collection.updateOne( {}, [{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }], From 561ccd4d5305598e20a200e2967d004df7cc5c70 Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Mon, 10 Jun 2019 14:32:20 -0400 Subject: [PATCH 3/5] change Array.prototype.forEach to for for proper return error value --- lib/collection.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/collection.js b/lib/collection.js index 4168205ed4..9922f16173 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -686,9 +686,14 @@ Collection.prototype.updateOne = function(filter, update, options, callback) { if (typeof options === 'function') (callback = options), (options = {}); options = options || {}; - const err = Array.isArray(update) - ? update.forEach(checkForAtomicOperators) - : 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); From c77d21daa67637ae1bc14df5040ffb8a214bd3a7 Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Mon, 10 Jun 2019 15:34:43 -0400 Subject: [PATCH 4/5] fix mongo version in test --- test/functional/collection_tests.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index 505c58aa1b..8ead2fcc45 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1901,7 +1901,10 @@ describe('Collection', function() { it('should correctly update with pipeline', { metadata: { - requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } + requires: { + topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'], + mongodb: '>=4.2.0' + } }, // The actual test we wish to run From 3658be31ed2660bf62dd1d29ba2fb3654d9a5608 Mon Sep 17 00:00:00 2001 From: Sam Pal Date: Mon, 10 Jun 2019 16:03:02 -0400 Subject: [PATCH 5/5] fix topology and null check --- test/functional/collection_tests.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/functional/collection_tests.js b/test/functional/collection_tests.js index 8ead2fcc45..44f28a5525 100644 --- a/test/functional/collection_tests.js +++ b/test/functional/collection_tests.js @@ -1901,10 +1901,7 @@ describe('Collection', function() { it('should correctly update with pipeline', { metadata: { - requires: { - topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'], - mongodb: '>=4.2.0' - } + requires: { mongodb: '>=4.2.0' } }, // The actual test we wish to run @@ -1923,7 +1920,7 @@ describe('Collection', function() { [{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }], configuration.writeConcernMax(), (err, r) => { - expect(err).to.equal(null); + expect(err).to.not.exist; expect(r.result.n).to.equal(0); client.close(done);