Skip to content

Commit

Permalink
feat(Update): add the ability to specify a pipeline to an update comm…
Browse files Browse the repository at this point in the history
…and (#2017)

* feat(Update): add the ability to specify a pipeline to an update command

NODE-1920
  • Loading branch information
Sam Pal authored and daprahamian committed Aug 13, 2019
1 parent c0b29c4 commit dc1387e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
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
32 changes: 32 additions & 0 deletions test/functional/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1918,4 +1918,36 @@ describe('Collection', function() {
client.close();
});
});

it('should correctly update with pipeline', {
metadata: {
requires: { 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.not.exist;
expect(r.result.n).to.equal(0);

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

0 comments on commit dc1387e

Please sign in to comment.