Skip to content

Commit

Permalink
fix: honor ignoreUndefined on findAndModify commands (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum authored Dec 14, 2020
1 parent db3f800 commit a25b67c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,12 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

// Add ignoreUndefined
if (this.s.options.ignoreUndefined) {
options = Object.assign({}, options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

return executeOperation(
this.s.topology,
new FindOneAndDeleteOperation(this, filter, options),
Expand Down Expand Up @@ -1722,6 +1728,12 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

// Add ignoreUndefined
if (this.s.options.ignoreUndefined) {
options = Object.assign({}, options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

return executeOperation(
this.s.topology,
new FindOneAndReplaceOperation(this, filter, replacement, options),
Expand Down Expand Up @@ -1757,6 +1769,12 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

// Add ignoreUndefined
if (this.s.options.ignoreUndefined) {
options = Object.assign({}, options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}

return executeOperation(
this.s.topology,
new FindOneAndUpdateOperation(this, filter, update, options),
Expand Down
42 changes: 42 additions & 0 deletions test/functional/find_and_modify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,46 @@ describe('Find and Modify', function() {
});
}
});

it('should honor ignoreUndefined on all findAndModify commands', function(done) {
const configuration = this.configuration;
const client = configuration.newClient({}, { ignoreUndefined: true });
client.connect((err, client) => {
expect(err).to.be.null;
const db = client.db(configuration.db);
const collection = db.collection('findAndModifyIgnoreUndefined');
collection.findOneAndUpdate(
{ test: 'test' },
{
$set: { undefined_1: undefined, null_1: null },
$setOnInsert: { undefined_2: undefined, null_2: null }
},
{ upsert: true },
err => {
expect(err).to.be.null;
collection.findOne({ test: 'test' }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.have.property('null_1', null);
expect(result).to.not.have.property('undefined_1');
expect(result).to.have.property('null_2', null);
expect(result).to.not.have.property('undefined_2');
collection.findOneAndReplace(
{ test: 'test' },
{ test: 'test', undefined_3: undefined, null_3: null },
(err, result) => {
expect(err).to.not.exist;
expect(result).to.exist;
collection.findOne({ test: 'test' }, (err, result) => {
expect(err).to.not.exist;
expect(result).to.have.property('null_3', null);
expect(result).to.not.have.property('undefined_3');
client.close(done);
});
}
);
});
}
);
});
});
});

0 comments on commit a25b67c

Please sign in to comment.