带有SubDocument字段的Mongoose Schema是必需的

时间:2014-09-28 14:04:35

标签: mongodb mongoose

在Joe Eames教程 “使用MEAN Stack构建AngularJS和Node.js应用程序” 之后,Pluralsight,他演示了一个Mongoose Schema示例使用所需的文档字段,如下所示:

var courseSchema = mongoose.Schema({
  title: {type:String, required:'{PATH} is required!'},
  featured: {type:Boolean, required:'{PATH} is required!'},
  published: {type:Date, required:'{PATH} is required!'},
  tags: [String]
   });

我创建了自己的架构,其子文档有效:

  ......
  skin : {styleNum : String, headerLogo: String},
  ......

但是,我希望字段是必需的,并且子文档不是数组(在Entity Framework中它将类似于关联,而不是集合),所以这是错误的:

  ......
  skin : [
          {styleNum : String, required:'{PATH} is required!'},
          {headerLogo: String, required:'{PATH} is required!'}
         ]
  ......

在需要两个字段的Mongoose Schema中创建“实体框架”类关联的正确语法是什么?

1 个答案:

答案 0 :(得分:0)

没关系,想出我的错误在哪里:

 skin : {
     styleNum: {type: String, required: '{PATH} is required!'},
     headerLogo: {type: String, required: '{PATH} is required!'}
 },