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(Update): add the ability to specify a pipeline to an update command #2017

Merged
merged 5 commits into from
Jun 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,15 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

const err = checkForAtomicOperators(update);
let err;
if (Array.isArray(update)) {
for (let i = 0; !err && i < update.length; i++) {
err = checkForAtomicOperators(update[i]);
}
} else {
err = checkForAtomicOperators(update);
}

if (err) {
if (typeof callback === 'function') return callback(err);
return this.s.promiseLibrary.reject(err);
Expand Down
35 changes: 35 additions & 0 deletions test/functional/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1898,4 +1898,39 @@ describe('Collection', function() {
}
});
});

it('should correctly update with pipeline', {
metadata: {
requires: {
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'],
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK we don't use the heap and wiredtiger environments anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can handle this by remove this entire line, which will cause this test to run for every topology that we run them against.

mongodb: '>=4.2.0'
}
},

// The actual test we wish to run
test: function(done) {
const configuration = this.configuration;
const client = configuration.newClient(configuration.writeConcernMax(), {
poolSize: 1
});

client.connect((err, client) => {
const db = client.db(configuration.db);

db.createCollection('test_should_correctly_do_update_with_pipeline', (err, collection) => {
collection.updateOne(
{},
[{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }],
configuration.writeConcernMax(),
(err, r) => {
expect(err).to.equal(null);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use expect(err).to.not.exist.

expect(r.result.n).to.equal(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we expect this to be 0?


client.close(done);
}
);
});
});
}
});
});