From e293fe2bd8a15220da756e18ad64e68e8cf9164f Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:19:33 -0400 Subject: [PATCH 1/4] works as expected --- test/document.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/document.test.js b/test/document.test.js index 994c049f3ed..a7d7b7c5ab0 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12407,6 +12407,20 @@ describe('document', function() { const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 }); assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A'); }); + + it('should ignore `id` if the object contains `id` and `_id` as keys (gh-13762)', async function () { + const testSchema = new Schema({ + _id: { + type: Number + } + }, { _id: false, id: false }); + const Test = db.model('Test', testSchema); + + const x = new Test({ _id: 1, id: 2}); + await x.save(); + const fromDb = await Test.findById(x._id).lean(); + assert.equal(fromDb._id, 1); + }); }); describe('Check if instance function that is supplied in schema option is availabe', function() { From bad9615e106c1393e931bdd4e267f4c1e63c4f2e Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:32:03 -0400 Subject: [PATCH 2/4] fix: `id` won't override `_id` --- lib/document.js | 3 +++ test/document.test.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/document.js b/lib/document.js index 712cd949202..52c7deedf54 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1020,6 +1020,9 @@ Document.prototype.overwrite = function overwrite(obj) { */ Document.prototype.$set = function $set(path, val, type, options) { + if (path._id && path.id) { + delete path.id; + } if (utils.isPOJO(type)) { options = type; type = undefined; diff --git a/test/document.test.js b/test/document.test.js index a7d7b7c5ab0..f5a233bed49 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12413,7 +12413,7 @@ describe('document', function() { _id: { type: Number } - }, { _id: false, id: false }); + }); const Test = db.model('Test', testSchema); const x = new Test({ _id: 1, id: 2}); @@ -12421,6 +12421,21 @@ describe('document', function() { const fromDb = await Test.findById(x._id).lean(); assert.equal(fromDb._id, 1); }); + it('should ignore `id` if settings with an object that contains `_id` and `id` (gh-13672)', async function() { + const testSchema = new Schema({ + _id: { + type: Number + } + }); + const Test = db.model('Test', testSchema); + + const x = new Test(); + x.set('_id', { _id: 1, id: 2 }); + await x.save(); + + const fromDb = await Test.findById(x._id).lean(); + assert.equal(fromDb._id, 1); + }) }); describe('Check if instance function that is supplied in schema option is availabe', function() { From 294d287acb29541fbddd5297669bd908769218f3 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:33:24 -0400 Subject: [PATCH 3/4] fix: lint --- test/document.test.js | 6 +++--- test/types/schema.test.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/document.test.js b/test/document.test.js index f5a233bed49..55f281da309 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -12408,7 +12408,7 @@ describe('document', function() { assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A'); }); - it('should ignore `id` if the object contains `id` and `_id` as keys (gh-13762)', async function () { + it('should ignore `id` if the object contains `id` and `_id` as keys (gh-13762)', async function() { const testSchema = new Schema({ _id: { type: Number @@ -12416,7 +12416,7 @@ describe('document', function() { }); const Test = db.model('Test', testSchema); - const x = new Test({ _id: 1, id: 2}); + const x = new Test({ _id: 1, id: 2 }); await x.save(); const fromDb = await Test.findById(x._id).lean(); assert.equal(fromDb._id, 1); @@ -12435,7 +12435,7 @@ describe('document', function() { const fromDb = await Test.findById(x._id).lean(); assert.equal(fromDb._id, 1); - }) + }); }); describe('Check if instance function that is supplied in schema option is availabe', function() { diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index b6726bfdc78..16f58ed938b 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1217,4 +1217,4 @@ function gh13800() { expectType(this.lastName); expectError(this.someOtherField); }); -} \ No newline at end of file +} From cf576fcd949b64d8eb26ff3e0907af2c41419420 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 12 Sep 2023 16:55:07 -0400 Subject: [PATCH 4/4] Update document.js --- lib/document.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/document.js b/lib/document.js index 52c7deedf54..abaf97582eb 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1020,9 +1020,6 @@ Document.prototype.overwrite = function overwrite(obj) { */ Document.prototype.$set = function $set(path, val, type, options) { - if (path._id && path.id) { - delete path.id; - } if (utils.isPOJO(type)) { options = type; type = undefined; @@ -1063,6 +1060,10 @@ Document.prototype.$set = function $set(path, val, type, options) { [path, val] = [val, path]; } + if ('_id' in path && 'id' in path) { + delete path.id; + } + prefix = val ? val + '.' : ''; keys = getKeysInSchemaOrder(this.$__schema, path);