Skip to content

Commit

Permalink
fix: insert version key when using insertMany even if `toObject.ver…
Browse files Browse the repository at this point in the history
…sionKey` set to false

Fix #14344
  • Loading branch information
vkarpov15 committed Feb 16, 2024
1 parent 1f340d4 commit ed8bad0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/options.js
Expand Up @@ -11,5 +11,6 @@ exports.internalToObjectOptions = {
_skipDepopulateTopLevel: true,
depopulate: true,
flattenDecimals: false,
useProjection: false
useProjection: false,
versionKey: true
};
15 changes: 15 additions & 0 deletions test/model.test.js
Expand Up @@ -7302,6 +7302,21 @@ describe('Model', function() {
/First argument to `Model` constructor must be an object/
);
});

it('inserts versionKey even if schema has `toObject.versionKey` set to false (gh-14344)', async function() {
const schema = new mongoose.Schema(
{ name: String },
{ versionKey: '__v', toObject: { versionKey: false } }
);

const Model = db.model('Test', schema);

await Model.insertMany([{ name: 'x' }]);

const doc = await Model.findOne();

assert.strictEqual(doc.__v, 0);
});
});


Expand Down

0 comments on commit ed8bad0

Please sign in to comment.