如何在shcema

时间:2018-02-12 19:42:54

标签: mongoose mongoose-schema

以下是一个例子:

    var aaaSchema = new mongoose.Schema({
        name: String,
    });

    var testSchema = new mongoose.Schema({
        test: mongoose.Schema.Types.Mixed,
    });

现在,test可以是任何Object,因为它是:mongoose.Schema.Types.Mixed 我想定义测试是:

   {
       aa: aaaSchema,
       bb: aaaSchema,
       ....
    }

这意味着密钥可以是任何字符串(aa,bb或其他),值必须是{name:String},这是aaaSchema。

1 个答案:

答案 0 :(得分:0)

Subdocuments可用于嵌入式文档。

var aaaSchema = new Schema({ name: 'string' });
var testSchema = new Schema({ test: mongoose.Schema.Types.Mixed });

var mainSchema = new Schema({
  aa: aaaSchema,
  bb: aaaSchema
});