diff --git a/lib/schema/documentArray.js b/lib/schema/documentArray.js index 2af276500ff..d35c5dfbf10 100644 --- a/lib/schema/documentArray.js +++ b/lib/schema/documentArray.js @@ -195,7 +195,7 @@ SchemaDocumentArray.prototype.discriminator = function(name, schema, options) { schema = schema.clone(); } - schema = discriminator(this.casterConstructor, name, schema, tiedValue); + schema = discriminator(this.casterConstructor, name, schema, tiedValue, null, null, options?.overwriteExisting); const EmbeddedDocument = _createConstructor(schema, null, this.casterConstructor); EmbeddedDocument.baseCasterConstructor = this.casterConstructor; diff --git a/test/model.test.js b/test/model.test.js index 840b098a86a..9d35f207000 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -7463,6 +7463,23 @@ describe('Model', function() { assert.equal(instance.item.whoAmI(), 'I am Test2'); }); + it('overwrites existing discriminators when calling recompileSchema (gh-14527) (gh-14444)', async function() { + const shopItemSchema = new mongoose.Schema({}, { discriminatorKey: 'type' }); + const shopSchema = new mongoose.Schema({ + items: { type: [shopItemSchema], required: true } + }); + + const shopItemSubType = new mongoose.Schema({ prop: Number }); + shopItemSchema.discriminator(2, shopItemSubType); + const shopModel = db.model('shop', shopSchema); + + shopModel.recompileSchema(); + const doc = new shopModel({ + items: [{ type: 2, prop: 42 }] + }); + assert.equal(doc.items[0].prop, 42); + }); + it('inserts versionKey even if schema has `toObject.versionKey` set to false (gh-14344)', async function() { const schema = new mongoose.Schema( { name: String },