Skip to content

Commit d099622

Browse files
authored
feat(NODE-2751): add arrayFilters builder to bulk FindOperators (#2820)
1 parent 53abfe7 commit d099622

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/bulk/common.ts

+10
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,16 @@ export class FindOperators {
803803
this.bulkOperation.s.currentOp.collation = collation;
804804
return this;
805805
}
806+
807+
/** Specifies arrayFilters for UpdateOne or UpdateMany bulk operations. */
808+
arrayFilters(arrayFilters: Document[]): this {
809+
if (!this.bulkOperation.s.currentOp) {
810+
this.bulkOperation.s.currentOp = {};
811+
}
812+
813+
this.bulkOperation.s.currentOp.arrayFilters = arrayFilters;
814+
return this;
815+
}
806816
}
807817

808818
/** @internal */

test/functional/bulk.test.js

+42
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,48 @@ describe('Bulk', function () {
19401940
})
19411941
});
19421942

1943+
it('should apply arrayFilters to bulk updates via FindOperators', {
1944+
metadata: { requires: { mongodb: '>= 3.6' } },
1945+
test: withMonitoredClient(['update', 'delete'], function (client, events, done) {
1946+
client.db().dropCollection('bulkArrayFilters', () => {
1947+
const coll = client.db().collection('bulkArrayFilters');
1948+
const bulk = coll.initializeOrderedBulkOp();
1949+
1950+
bulk.insert({ person: 'Foo', scores: [4, 9, 12] });
1951+
bulk.insert({ person: 'Bar', scores: [13, 0, 52] });
1952+
bulk
1953+
.find({ scores: { $lt: 1 } })
1954+
.arrayFilters([{ e: { $lt: 1 } }])
1955+
.updateOne({ $set: { 'scores.$[e]': 1 } });
1956+
bulk
1957+
.find({ scores: { $gte: 10 } })
1958+
.arrayFilters([{ e: { $gte: 10 } }])
1959+
.update({ $set: { 'scores.$[e]': 10 } });
1960+
1961+
bulk.execute(err => {
1962+
expect(err).to.not.exist;
1963+
expect(events).to.be.an('array').with.lengthOf(1);
1964+
expect(events[0]).to.have.property('commandName', 'update');
1965+
const updateCommand = events[0].command;
1966+
expect(updateCommand).property('updates').to.be.an('array').with.lengthOf(2);
1967+
updateCommand.updates.forEach(update => expect(update).to.have.property('arrayFilters'));
1968+
coll.find({}).toArray((err, result) => {
1969+
expect(err).to.not.exist;
1970+
expect(result[0]).to.containSubset({
1971+
person: 'Foo',
1972+
scores: [4, 9, 10]
1973+
});
1974+
expect(result[1]).to.containSubset({
1975+
person: 'Bar',
1976+
scores: [10, 1, 10]
1977+
});
1978+
client.close(done);
1979+
});
1980+
});
1981+
});
1982+
})
1983+
});
1984+
19431985
it('should throw an error if raw operations are passed to bulkWrite', function () {
19441986
const client = this.configuration.newClient();
19451987
return client.connect().then(() => {

0 commit comments

Comments
 (0)