From b97d410d5eca61d94a8376d0b0b34df7b9c96063 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 11 Sep 2022 19:24:32 -0400 Subject: [PATCH] fix(array): avoid using default `_id` when using pull() Fix #12294 --- lib/types/array/methods/index.js | 6 +++++- test/types.array.test.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/types/array/methods/index.js b/lib/types/array/methods/index.js index 127afd6c2ff..61f7b10d7d7 100644 --- a/lib/types/array/methods/index.js +++ b/lib/types/array/methods/index.js @@ -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); diff --git a/test/types.array.test.js b/test/types.array.test.js index de9afdbf6d8..8aef5ed7043 100644 --- a/test/types.array.test.js +++ b/test/types.array.test.js @@ -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() {