Skip to content

Commit

Permalink
Merge pull request #14938 from Automattic/vkarpov15/gh-14935
Browse files Browse the repository at this point in the history
fix: set flattenObjectIds to false when calling toObject() for internal purposes
  • Loading branch information
vkarpov15 authored Oct 9, 2024
2 parents ee6b861 + 6aecc01 commit d861d21
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
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
};
23 changes: 23 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13927,6 +13927,29 @@ describe('document', function() {
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
}
},
{
toObject: { flattenObjectIds: true }
}
);
const Test = db.model('Test', TestSchema);

const professionalId = new mongoose.Types.ObjectId();
await Test.insertMany([{ professionalId, firstName: 'test' }]);

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: {
Expand Down

0 comments on commit d861d21

Please sign in to comment.