我有一个带有嵌入式鉴别器的方案,我想克隆它。但是,当我从此克隆模式创建模型并尝试创建文档时,与该标识符相关的某些属性就消失了。
代码在这里
const mongoose = require('mongoose');
const propertiesSchema = new mongoose.Schema({
name: { type: String },
},
{ discriminatorKey: 'type', _id: false });
const collectionSchema = new mongoose.Schema({
name: { type: String },
properties: [propertiesSchema]
});
const propertiesArray = collectionSchema.path(`properties`);
propertiesArray.discriminator(`type1`,
new mongoose.Schema({ type1: String }, { _id: false })
);
propertiesArray.discriminator(`type2`,
new mongoose.Schema({ type2: String }, { _id: false })
);
const Collection = new mongoose.model('Collection', collectionSchema);
const Clone = new mongoose.model('Clone', Collection.schema.clone());
const data = {
name: "Collection",
properties: [
{ type: "type1", type1: "type1-1" },
{ type: "type2", type2: "type2-2" }
]
}
console.log(new Collection(data));
console.log(new Clone(data));
结果是:
{ _id: 5d1b583e6d2d8b519c8849b8,
name: 'Collection',
properties:
[ { type: 'type1', type1: 'type1-1' },
{ type: 'type2', type2: 'type2-2' } ] }
{ _id: 5d1b583e6d2d8b519c8849b9,
name: 'Collection',
properties: [ { type: 'type1' }, { type: 'type2' } ] }
问题是-为什么文档不同,如何正确克隆或在克隆方案中“重新应用”鉴别符?
节点:v10.15.3 猫鼬:5.6.2