Skip to content

Commit

Permalink
fix(array): avoid adding defaults to query filter when using pull()
Browse files Browse the repository at this point in the history
… on a doc array with no `_id`

Fix #12294
  • Loading branch information
vkarpov15 committed Sep 11, 2022
1 parent e8ac7d7 commit 40b94c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/types/array/methods/index.js
Expand Up @@ -341,8 +341,21 @@ const methods = {
const pullOp = atomics['$pull'] || (atomics['$pull'] = {});
if (val[0] instanceof ArraySubdocument) {
selector = pullOp['$or'] || (pullOp['$or'] = []);
Array.prototype.push.apply(selector, val.map(function(v) {
return v.toObject({ transform: false, virtuals: false });
Array.prototype.push.apply(selector, val.map(v => {
return v.toObject({
transform: (doc, ret) => {
if (v == null || v.$__ == null) {
return ret;
}

Object.keys(v.$__.activePaths.getStatePaths('default')).forEach(path => {
delete ret[path];
});

return ret;
},
virtuals: false
});
}));
} else {
selector = pullOp['_id'] || (pullOp['_id'] = { $in: [] });
Expand Down
24 changes: 24 additions & 0 deletions test/types.array.test.js
Expand Up @@ -822,6 +822,30 @@ describe('types array', function() {
});
});
});

it('avoids adding default paths to query filter (gh-12294)', async function() {
const catschema = new Schema({
name: String,
colors: [{
_id: false,
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 40b94c0

Please sign in to comment.