Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-2751): add arrayFilters builder to bulk FindOperators #2820

Merged
merged 5 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 additions & 0 deletions test/functional/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,50 @@ 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.length(1);
expect(events[0]).property('commandName').to.equal('update');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a note: the property assertions can take the property name and value to compare directly, so this line is equivalent to expect(events[0]).to.have.property('commandName', 'update'); and if you go a step further and use the nested keyword, you could say expect(events).to.have.nested.property('0.commandName', 'update')

these are personal preferences, I just wanted to make you aware of the options in case any of them appeal to you

also be careful with the length assertion, since it can't be reliably chained in all cases (e.g., you can't chain it off a), so chai's own docs recommend to use lengthOf as a general rule, since that does work exactly as expected in all cases

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(() => {
Expand Down