Node.js 如何使用mongoosejs用鉴别器覆盖父模式字段

Node.js 如何使用mongoosejs用鉴别器覆盖父模式字段,node.js,mongodb,express,mongoose,schema,Node.js,Mongodb,Express,Mongoose,Schema,我有一个父模式帖子: { title: { type: String, required: true }, authors: { type: [String], required: true } } 我想使用discriminator()mongoose函数使子模式继承此模式,但我想在authors字段上添加额外的验证,以确保数组不为空 我该怎么做 另外,有谁能告诉我关于猫鼬鉴别器的好文件吗。官方

我有一个父模式帖子:

{
    title: {
        type: String,
        required: true
    },
    authors: {
        type: [String],
        required: true
    }
 }
我想使用discriminator()mongoose函数使子模式继承此模式,但我想在authors字段上添加额外的验证,以确保数组不为空

我该怎么做

另外,有谁能告诉我关于猫鼬鉴别器的好文件吗。官方文档对我的搜索没有帮助。

这对你有帮助吗

var Parent = mongoose.model('Parent', new mongoose.Schema({
    title: {...},
    author: {...}
}))

var Child = Parent.discriminator('Child', new mongoose.Schema({
    author: {
        default: ['something']
    }
}))

请注意,您必须在子属性之前包含父属性(显然是:o)

Ah ok,这样父属性就可以被子属性覆盖?是的,您可以插入新字段或将整个字段重写为其他类型。不过,请小心重写字段。如果您发现自己经常重写字段并更改受歧视类中的类型,您可能需要重新考虑您的设计,并可能将该字段的定义推到“子类”中