From d099622cdd1ba60d108b1b6a1b323dff847f99b5 Mon Sep 17 00:00:00 2001 From: Eric Adum Date: Fri, 28 May 2021 09:45:16 -0400 Subject: [PATCH] feat(NODE-2751): add arrayFilters builder to bulk FindOperators (#2820) --- src/bulk/common.ts | 10 +++++++++ test/functional/bulk.test.js | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/bulk/common.ts b/src/bulk/common.ts index b0d407885b..1b68124a52 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 arrayFilters for UpdateOne or UpdateMany bulk operations. */ + 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 3260e937c5..63b58efcc3 100644 --- a/test/functional/bulk.test.js +++ b/test/functional/bulk.test.js @@ -1940,6 +1940,48 @@ describe('Bulk', function () { }) }); + it('should apply arrayFilters to bulk updates via FindOperators', { + metadata: { requires: { mongodb: '>= 3.6' } }, + 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] }); + 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.lengthOf(1); + expect(events[0]).to.have.property('commandName', 'update'); + const updateCommand = events[0].command; + 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({ + 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(() => {