Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: set flattenObjectIds to false when calling toObject() for intern…
…al purposes

Fix #14935
  • Loading branch information
vkarpov15 committed Oct 6, 2024
commit 47c3b469c26635e7c8e9bbe652ccfcf7c0a4e346
3 changes: 1 addition & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ exports.internalToObjectOptions = {
depopulate: true,
flattenDecimals: false,
useProjection: false,
versionKey: true
versionKey: true,
flattenObjectIds: false
};
26 changes: 26 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down