Skip to content

Commit

Permalink
fix(array): avoid using default _id when using pull()
Browse files Browse the repository at this point in the history
Fix #12294
  • Loading branch information
vkarpov15 committed Sep 11, 2022
1 parent 40b94c0 commit b97d410
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/types/array/methods/index.js
Expand Up @@ -603,7 +603,11 @@ const methods = {

if (values[0] instanceof ArraySubdocument) {
this._registerAtomic('$pullDocs', values.map(function(v) {
return v.$__getValue('_id') || v;
const _id = v.$__getValue('_id');
if (_id === undefined || v.$isDefault('_id')) {
return v;
}
return _id;
}));
} else {
this._registerAtomic('$pullAll', values);
Expand Down
23 changes: 23 additions & 0 deletions test/types.array.test.js
Expand Up @@ -846,6 +846,29 @@ describe('types array', function() {
{ $or: [{ name: 'Orange' }] }
]]);
});

it('avoids adding default paths to query filter with _id (gh-12294)', async function() {
const catschema = new Schema({
name: String,
colors: [{
hex: { type: String, default: '#ffffff' },
name: String
}]
});
const Cat = db.model('Test', catschema);

const cat = new Cat({});
cat.init({
name: 'Garfield',
colors: [{ name: 'Orange' }]
});

cat.colors.pull({ name: 'Orange' });
assert.deepStrictEqual(cat.colors.$__getAtomics(), [[
'$pull',
{ $or: [{ name: 'Orange' }] }
]]);
});
});

describe('$pop()', function() {
Expand Down

0 comments on commit b97d410

Please sign in to comment.