Skip to content

Commit

Permalink
feat(NODE-2751): add arrayFilters builder to bulk FindOperators (#2820)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum authored May 28, 2021
1 parent 53abfe7 commit d099622
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
42 changes: 42 additions & 0 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down

0 comments on commit d099622

Please sign in to comment.