我想在Mongoose模型中有一个条件SubDocument。
在下面的代码中,我尝试传达我想要做的事情。
var SubSchemaA = new Schema({
address: { type:String, required:true }
});
var SubSchemaB = new Schema({
birthday: { type:Date, required:true }
});
var MainSchema = new Schema({
type: String, enum: ['A', 'B'],
content: Schema, enum: [SubSchemaA, SubSchemaB]
});
MainSchema.pre('validate', function (next) {
var content = this.content;
if (this.type == 'A') {
this.content = SubSchemaA(content)
} else {
this.content = SubSchemaB(content)
}
next()
});