如何在shcema中定义mongoose模式

时间:2022-04-26 18:32:35

Here is an example:

这是一个例子:

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

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

For now, test could be any Object since it is: mongoose.Schema.Types.Mixed I want to define test is:

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

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

It means that the key could be any string (aa, bb, or others), the value must be {name: String} which is aaaSchema.

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

1 个解决方案

#1


0  

Subdocuments can be used for embedded documents.

子文档可用于嵌入式文档。

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

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

#1


0  

Subdocuments can be used for embedded documents.

子文档可用于嵌入式文档。

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

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