From 47c3b469c26635e7c8e9bbe652ccfcf7c0a4e346 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 6 Oct 2024 14:06:58 -0400 Subject: [PATCH 1/4] fix: set flattenObjectIds to false when calling toObject() for internal purposes Fix #14935 --- lib/model.js | 3 +-- lib/options.js | 3 ++- test/document.test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/model.js b/lib/model.js index 1c361bcb495..e2c4d68c7fa 100644 --- a/lib/model.js +++ b/lib/model.js @@ -75,8 +75,7 @@ const subclassedSymbol = Symbol('mongoose#Model#subclassed'); const { VERSION_INC, VERSION_WHERE, VERSION_ALL } = Document; const saveToObjectOptions = Object.assign({}, internalToObjectOptions, { - bson: true, - flattenObjectIds: false + bson: true }); /** diff --git a/lib/options.js b/lib/options.js index 3bae58e1200..bbdcda8b97e 100644 --- a/lib/options.js +++ b/lib/options.js @@ -12,5 +12,6 @@ exports.internalToObjectOptions = { depopulate: true, flattenDecimals: false, useProjection: false, - versionKey: true + versionKey: true, + flattenObjectIds: false }; diff --git a/test/document.test.js b/test/document.test.js index 7150ffe64b4..84f0a73ebf0 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -13926,6 +13926,32 @@ describe('document', function() { cur.subdocs[0] = { test: 'updated' }; await savedDoc.save(); }); + + it('avoids flattening objectids on insertMany (gh-14935)', async function() { + const TestSchema = new Schema( + { + professionalId: { + type: Schema.Types.ObjectId + }, + firstName: { + type: String + }, + nested: { + test: String + } + }, + { + toObject: { flattenObjectIds: true } + } + ); + const Test = db.model('Test', TestSchema); + + const professionalId = new mongoose.Types.ObjectId(); + await Test.insertMany([{ professionalId, name: 'test' }]); + + const doc = await Test.findOne({ professionalId }).lean().orFail(); + assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId); + }); }); describe('Check if instance function that is supplied in schema option is available', function() { From 80441c00f5bb8e192fe1f16c49c5c3dca1826e4a Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 8 Oct 2024 12:54:54 -0400 Subject: [PATCH 2/4] style: fix lint --- test/document.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index b08abad27ee..6aa5e98413d 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -13952,7 +13952,7 @@ describe('document', function() { const doc = await Test.findOne({ professionalId }).lean().orFail(); assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId); }); - + it('handles buffers stored as EJSON POJO (gh-14911)', async function() { const pdfSchema = new mongoose.Schema({ pdfSettings: { From 8068c6a61d3ad7e1b748cc7101147d136bae0cdd Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 9 Oct 2024 11:22:50 -0400 Subject: [PATCH 3/4] Update test/document.test.js Co-authored-by: hasezoey --- test/document.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/document.test.js b/test/document.test.js index 6aa5e98413d..b12f19ea686 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -13947,7 +13947,7 @@ describe('document', function() { const Test = db.model('Test', TestSchema); const professionalId = new mongoose.Types.ObjectId(); - await Test.insertMany([{ professionalId, name: 'test' }]); + await Test.insertMany([{ professionalId, firstName: 'test' }]); const doc = await Test.findOne({ professionalId }).lean().orFail(); assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId); From 6aecc016e84904d9dfce278b5458c7edc35b83da Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 9 Oct 2024 11:22:56 -0400 Subject: [PATCH 4/4] Update test/document.test.js Co-authored-by: hasezoey --- test/document.test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/document.test.js b/test/document.test.js index b12f19ea686..f3869b8e58c 100644 --- a/test/document.test.js +++ b/test/document.test.js @@ -13935,9 +13935,6 @@ describe('document', function() { }, firstName: { type: String - }, - nested: { - test: String } }, {