Skip to content

Commit a25b67c

Browse files
authored
fix: honor ignoreUndefined on findAndModify commands (#2671)
NODE-2945
1 parent db3f800 commit a25b67c

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/collection.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,12 @@ Collection.prototype.findOneAndDelete = function(filter, options, callback) {
16881688
if (typeof options === 'function') (callback = options), (options = {});
16891689
options = options || {};
16901690

1691+
// Add ignoreUndefined
1692+
if (this.s.options.ignoreUndefined) {
1693+
options = Object.assign({}, options);
1694+
options.ignoreUndefined = this.s.options.ignoreUndefined;
1695+
}
1696+
16911697
return executeOperation(
16921698
this.s.topology,
16931699
new FindOneAndDeleteOperation(this, filter, options),
@@ -1722,6 +1728,12 @@ Collection.prototype.findOneAndReplace = function(filter, replacement, options,
17221728
if (typeof options === 'function') (callback = options), (options = {});
17231729
options = options || {};
17241730

1731+
// Add ignoreUndefined
1732+
if (this.s.options.ignoreUndefined) {
1733+
options = Object.assign({}, options);
1734+
options.ignoreUndefined = this.s.options.ignoreUndefined;
1735+
}
1736+
17251737
return executeOperation(
17261738
this.s.topology,
17271739
new FindOneAndReplaceOperation(this, filter, replacement, options),
@@ -1757,6 +1769,12 @@ Collection.prototype.findOneAndUpdate = function(filter, update, options, callba
17571769
if (typeof options === 'function') (callback = options), (options = {});
17581770
options = options || {};
17591771

1772+
// Add ignoreUndefined
1773+
if (this.s.options.ignoreUndefined) {
1774+
options = Object.assign({}, options);
1775+
options.ignoreUndefined = this.s.options.ignoreUndefined;
1776+
}
1777+
17601778
return executeOperation(
17611779
this.s.topology,
17621780
new FindOneAndUpdateOperation(this, filter, update, options),

test/functional/find_and_modify.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,46 @@ describe('Find and Modify', function() {
226226
});
227227
}
228228
});
229+
230+
it('should honor ignoreUndefined on all findAndModify commands', function(done) {
231+
const configuration = this.configuration;
232+
const client = configuration.newClient({}, { ignoreUndefined: true });
233+
client.connect((err, client) => {
234+
expect(err).to.be.null;
235+
const db = client.db(configuration.db);
236+
const collection = db.collection('findAndModifyIgnoreUndefined');
237+
collection.findOneAndUpdate(
238+
{ test: 'test' },
239+
{
240+
$set: { undefined_1: undefined, null_1: null },
241+
$setOnInsert: { undefined_2: undefined, null_2: null }
242+
},
243+
{ upsert: true },
244+
err => {
245+
expect(err).to.be.null;
246+
collection.findOne({ test: 'test' }, (err, result) => {
247+
expect(err).to.not.exist;
248+
expect(result).to.have.property('null_1', null);
249+
expect(result).to.not.have.property('undefined_1');
250+
expect(result).to.have.property('null_2', null);
251+
expect(result).to.not.have.property('undefined_2');
252+
collection.findOneAndReplace(
253+
{ test: 'test' },
254+
{ test: 'test', undefined_3: undefined, null_3: null },
255+
(err, result) => {
256+
expect(err).to.not.exist;
257+
expect(result).to.exist;
258+
collection.findOne({ test: 'test' }, (err, result) => {
259+
expect(err).to.not.exist;
260+
expect(result).to.have.property('null_3', null);
261+
expect(result).to.not.have.property('undefined_3');
262+
client.close(done);
263+
});
264+
}
265+
);
266+
});
267+
}
268+
);
269+
});
270+
});
229271
});

0 commit comments

Comments
 (0)