Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongoose中的条件子文档_Mongoose - Fatal编程技术网

Mongoose中的条件子文档

Mongoose中的条件子文档,mongoose,Mongoose,我想要一个Mongoose模型中的条件子文档 在下面的代码中,我试图传达我想要做的事情 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: [

我想要一个Mongoose模型中的条件子文档

在下面的代码中,我试图传达我想要做的事情

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()
});

看起来你在找猫鼬的。尽管如此,为什么不为模式提供这两个属性呢?因为MongoDB只在有值的情况下添加一个属性。如果我同时提供这两个字段,例如“必填”字段将出现问题。如果问题是要确保其中一个字段已填充,您可以在中间件中执行此操作。请进一步解释您的期望。是的,我可以在中间件中执行此操作,但如果我有很多字段,则需要一些字段,有些具有枚举值等。它变得非常复杂。