Skip to content

Commit dc1387e

Browse files
Sam Paldaprahamian
Sam Pal
authored andcommitted
feat(Update): add the ability to specify a pipeline to an update command (#2017)
* feat(Update): add the ability to specify a pipeline to an update command NODE-1920
1 parent c0b29c4 commit dc1387e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/collection.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,15 @@ Collection.prototype.updateOne = function(filter, update, options, callback) {
686686
if (typeof options === 'function') (callback = options), (options = {});
687687
options = options || {};
688688

689-
const err = checkForAtomicOperators(update);
689+
let err;
690+
if (Array.isArray(update)) {
691+
for (let i = 0; !err && i < update.length; i++) {
692+
err = checkForAtomicOperators(update[i]);
693+
}
694+
} else {
695+
err = checkForAtomicOperators(update);
696+
}
697+
690698
if (err) {
691699
if (typeof callback === 'function') return callback(err);
692700
return this.s.promiseLibrary.reject(err);

test/functional/collection_tests.js

+32
Original file line numberDiff line numberDiff line change
@@ -1918,4 +1918,36 @@ describe('Collection', function() {
19181918
client.close();
19191919
});
19201920
});
1921+
1922+
it('should correctly update with pipeline', {
1923+
metadata: {
1924+
requires: { mongodb: '>=4.2.0' }
1925+
},
1926+
1927+
// The actual test we wish to run
1928+
test: function(done) {
1929+
const configuration = this.configuration;
1930+
const client = configuration.newClient(configuration.writeConcernMax(), {
1931+
poolSize: 1
1932+
});
1933+
1934+
client.connect((err, client) => {
1935+
const db = client.db(configuration.db);
1936+
1937+
db.createCollection('test_should_correctly_do_update_with_pipeline', (err, collection) => {
1938+
collection.updateOne(
1939+
{},
1940+
[{ $set: { a: 1 } }, { $set: { b: 1 } }, { $set: { d: 1 } }],
1941+
configuration.writeConcernMax(),
1942+
(err, r) => {
1943+
expect(err).to.not.exist;
1944+
expect(r.result.n).to.equal(0);
1945+
1946+
client.close(done);
1947+
}
1948+
);
1949+
});
1950+
});
1951+
}
1952+
});
19211953
});

0 commit comments

Comments
 (0)