Skip to content

Commit

Permalink
fix(findOneAndUpdate): ensure that update documents contain atomic op…
Browse files Browse the repository at this point in the history
…erators

Fixes NODE-1470
  • Loading branch information
kvwalker authored May 23, 2018
1 parent 49fbafd commit eb68074
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,12 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
if (update == null || typeof update !== 'object')
throw toError('update parameter must be an object');

const err = checkForAtomicOperators(update);
if (err) {
if (typeof callback === 'function') return callback(err);
return this.s.promiseLibrary.reject(err);
}

return executeOperation(this.s.topology, findOneAndUpdate, [
this,
filter,
Expand Down
29 changes: 29 additions & 0 deletions test/functional/crud_api_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,35 @@ describe('CRUD API', function() {
}
});

it('should correctly throw error if update doc for findOneAndUpdate lacks atomic operator', function(done) {
let configuration = this.configuration;
let client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function(err, client) {
expect(err).to.not.exist;
let db = client.db(configuration.db);
let col = db.collection('t21_1');
col.insertOne({ a: 1, b: 2, c: 3 }, function(err, r) {
expect(err).to.not.exist;
expect(r.insertedCount).to.equal(1);

// empty update document
col.findOneAndUpdate({ a: 1 }, {}, function(err, r) {
expect(err).to.exist;
expect(r).to.not.exist;

// update document non empty but still lacks atomic operator
col.findOneAndUpdate({ a: 1 }, { b: 5 }, function(err, r) {
expect(err).to.exist;
expect(r).to.not.exist;

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

it('should correctly throw error if update doc for updateOne lacks atomic operator', {
// Add a tag that our runner can trigger on
// in this case we are setting that node needs to be higher than 0.10.X to run
Expand Down

0 comments on commit eb68074

Please sign in to comment.