From e507a638c8e840a774e169b2c12905e5611a5fb2 Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 25 May 2021 14:51:17 -0400 Subject: [PATCH 1/4] feat(bulk): add arrayFilters builder to bulk FindOperators --- src/bulk/common.ts | 10 ++++++++ test/functional/bulk.test.js | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index b0d407885ba..9417bd45de1 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -803,6 +803,16 @@ export class FindOperators { this.bulkOperation.s.currentOp.collation = collation; return this; } + + /** Specifies the collation for the query condition. */ + arrayFilters(arrayFilters: Document[]): this { + if (!this.bulkOperation.s.currentOp) { + this.bulkOperation.s.currentOp = {}; + } + + this.bulkOperation.s.currentOp.arrayFilters = arrayFilters; + return this; + } } /** @internal */ diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index 3260e937c5b..ce8d33d31f6 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1940,6 +1940,53 @@ describe('Bulk', function () { }) }); + it('should apply arrayFilters to bulk updates via FindOperators', { + metadata: { requires: { mongodb: '>= 3.4' } }, + test: withMonitoredClient(['update', 'delete'], function (client, events, done) { + client.db().dropCollection('bulkArrayFilters', () => { + const coll = client.db().collection('bulkArrayFilters'); + const bulk = coll.initializeOrderedBulkOp(); + + bulk.insert({ person: 'Foo', scores: [4, 9, 12] }); + bulk.insert({ person: 'Bar', scores: [13, 0, 52] }); + + // updates + bulk + .find({ scores: { $lt: 1 } }) + .arrayFilters([{ e: { $lt: 1 } }]) + .updateOne({ $set: { 'scores.$[e]': 1 } }); + + bulk + .find({ scores: { $gte: 10 } }) + .arrayFilters([{ e: { $gte: 10 } }]) + .update({ $set: { 'scores.$[e]': 10 } }); + + bulk.execute(err => { + expect(err).to.not.exist; + expect(events).to.be.an('array').with.length(1); + expect(events[0]).property('commandName').to.equal('update'); + const updateCommand = events[0].command; + expect(updateCommand).property('updates').to.be.an('array').with.length(2); + updateCommand.updates.forEach(update => { + expect(update).to.have.property('arrayFilters'); + }); + coll.find({}).toArray((err, result) => { + expect(err).to.not.exist; + expect(result[0]).to.containSubset({ + person: 'Foo', + scores: [4, 9, 10] + }); + expect(result[1]).to.containSubset({ + person: 'Bar', + scores: [10, 1, 10] + }); + client.close(done); + }); + }); + }); + }) + }); + it('should throw an error if raw operations are passed to bulkWrite', function () { const client = this.configuration.newClient(); return client.connect().then(() => { From 123e0b477efecfb92c1ce9a0bcbc18af02ecb9e7 Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 25 May 2021 15:54:39 -0400 Subject: [PATCH 2/4] fix min version on test --- test/functional/bulk.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index ce8d33d31f6..e26879bada9 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1941,7 +1941,7 @@ describe('Bulk', function () { }); it('should apply arrayFilters to bulk updates via FindOperators', { - metadata: { requires: { mongodb: '>= 3.4' } }, + metadata: { requires: { mongodb: '>= 3.6' } }, test: withMonitoredClient(['update', 'delete'], function (client, events, done) { client.db().dropCollection('bulkArrayFilters', () => { const coll = client.db().collection('bulkArrayFilters'); From 055bb7cdcc47fa739aef73bef5197aa3fe4f745e Mon Sep 17 00:00:00 2001 From: emadum Date: Tue, 25 May 2021 17:08:01 -0400 Subject: [PATCH 3/4] cleanup --- src/bulk/common.ts | 2 +- test/functional/bulk.test.js | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 9417bd45de1..1b68124a526 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -804,7 +804,7 @@ export class FindOperators { return this; } - /** Specifies the collation for the query condition. */ + /** Specifies arrayFilters for UpdateOne or UpdateMany bulk operations. */ arrayFilters(arrayFilters: Document[]): this { if (!this.bulkOperation.s.currentOp) { this.bulkOperation.s.currentOp = {}; diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index e26879bada9..a92a7cf25b7 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1949,13 +1949,10 @@ describe('Bulk', function () { bulk.insert({ person: 'Foo', scores: [4, 9, 12] }); bulk.insert({ person: 'Bar', scores: [13, 0, 52] }); - - // updates bulk .find({ scores: { $lt: 1 } }) .arrayFilters([{ e: { $lt: 1 } }]) .updateOne({ $set: { 'scores.$[e]': 1 } }); - bulk .find({ scores: { $gte: 10 } }) .arrayFilters([{ e: { $gte: 10 } }]) From 77bd4bc2feadfbb41ac19d353ae9b27377a6276e Mon Sep 17 00:00:00 2001 From: emadum Date: Fri, 28 May 2021 09:43:24 -0400 Subject: [PATCH 4/4] review feedback --- test/functional/bulk.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/functional/bulk.test.js b/test/functional/bulk.test.js index a92a7cf25b7..63b58efcc38 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1960,13 +1960,11 @@ describe('Bulk', function () { bulk.execute(err => { expect(err).to.not.exist; - expect(events).to.be.an('array').with.length(1); - expect(events[0]).property('commandName').to.equal('update'); + expect(events).to.be.an('array').with.lengthOf(1); + expect(events[0]).to.have.property('commandName', 'update'); const updateCommand = events[0].command; - expect(updateCommand).property('updates').to.be.an('array').with.length(2); - updateCommand.updates.forEach(update => { - expect(update).to.have.property('arrayFilters'); - }); + expect(updateCommand).property('updates').to.be.an('array').with.lengthOf(2); + updateCommand.updates.forEach(update => expect(update).to.have.property('arrayFilters')); coll.find({}).toArray((err, result) => { expect(err).to.not.exist; expect(result[0]).to.containSubset({