Skip to content

Commit eb68074

Browse files
authored
fix(findOneAndUpdate): ensure that update documents contain atomic operators
Fixes NODE-1470
1 parent 49fbafd commit eb68074

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/collection.js

+6
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,12 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
21072107
if (update == null || typeof update !== 'object')
21082108
throw toError('update parameter must be an object');
21092109

2110+
const err = checkForAtomicOperators(update);
2111+
if (err) {
2112+
if (typeof callback === 'function') return callback(err);
2113+
return this.s.promiseLibrary.reject(err);
2114+
}
2115+
21102116
return executeOperation(this.s.topology, findOneAndUpdate, [
21112117
this,
21122118
filter,

test/functional/crud_api_tests.js

+29
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,35 @@ describe('CRUD API', function() {
10231023
}
10241024
});
10251025

1026+
it('should correctly throw error if update doc for findOneAndUpdate lacks atomic operator', function(done) {
1027+
let configuration = this.configuration;
1028+
let client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
1029+
client.connect(function(err, client) {
1030+
expect(err).to.not.exist;
1031+
let db = client.db(configuration.db);
1032+
let col = db.collection('t21_1');
1033+
col.insertOne({ a: 1, b: 2, c: 3 }, function(err, r) {
1034+
expect(err).to.not.exist;
1035+
expect(r.insertedCount).to.equal(1);
1036+
1037+
// empty update document
1038+
col.findOneAndUpdate({ a: 1 }, {}, function(err, r) {
1039+
expect(err).to.exist;
1040+
expect(r).to.not.exist;
1041+
1042+
// update document non empty but still lacks atomic operator
1043+
col.findOneAndUpdate({ a: 1 }, { b: 5 }, function(err, r) {
1044+
expect(err).to.exist;
1045+
expect(r).to.not.exist;
1046+
1047+
client.close();
1048+
done();
1049+
});
1050+
});
1051+
});
1052+
});
1053+
});
1054+
10261055
it('should correctly throw error if update doc for updateOne lacks atomic operator', {
10271056
// Add a tag that our runner can trigger on
10281057
// in this case we are setting that node needs to be higher than 0.10.X to run

0 commit comments

Comments
 (0)