Skip to content

Commit d861d21

Browse files
authored
Merge pull request Automattic#14938 from Automattic/vkarpov15/Automatticgh-14935
fix: set flattenObjectIds to false when calling toObject() for internal purposes
2 parents ee6b861 + 6aecc01 commit d861d21

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/model.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ const subclassedSymbol = Symbol('mongoose#Model#subclassed');
7575
const { VERSION_INC, VERSION_WHERE, VERSION_ALL } = Document;
7676

7777
const saveToObjectOptions = Object.assign({}, internalToObjectOptions, {
78-
bson: true,
79-
flattenObjectIds: false
78+
bson: true
8079
});
8180

8281
/**

lib/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ exports.internalToObjectOptions = {
1212
depopulate: true,
1313
flattenDecimals: false,
1414
useProjection: false,
15-
versionKey: true
15+
versionKey: true,
16+
flattenObjectIds: false
1617
};

test/document.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13927,6 +13927,29 @@ describe('document', function() {
1392713927
await savedDoc.save();
1392813928
});
1392913929

13930+
it('avoids flattening objectids on insertMany (gh-14935)', async function() {
13931+
const TestSchema = new Schema(
13932+
{
13933+
professionalId: {
13934+
type: Schema.Types.ObjectId
13935+
},
13936+
firstName: {
13937+
type: String
13938+
}
13939+
},
13940+
{
13941+
toObject: { flattenObjectIds: true }
13942+
}
13943+
);
13944+
const Test = db.model('Test', TestSchema);
13945+
13946+
const professionalId = new mongoose.Types.ObjectId();
13947+
await Test.insertMany([{ professionalId, firstName: 'test' }]);
13948+
13949+
const doc = await Test.findOne({ professionalId }).lean().orFail();
13950+
assert.ok(doc.professionalId instanceof mongoose.Types.ObjectId);
13951+
});
13952+
1393013953
it('handles buffers stored as EJSON POJO (gh-14911)', async function() {
1393113954
const pdfSchema = new mongoose.Schema({
1393213955
pdfSettings: {

0 commit comments

Comments
 (0)