猫鼬模式 - 必需的孩子,而不是父母

时间:2016-07-06 08:01:19

标签: mongoose mongoose-schema

我的导航架构中有以下对象:

const navItem = new Schema ({
    "title": { type: string, unique: true, required: "A Title is req."},
    .............
    "badge": {
       "label": "string",
       "class": "string"
     }
    ..............
  })

虽然不需要徽章,但标签和班级是否存在徽章。

如何在我的猫鼬模式中正确显示可选徽章的此要求?

谢谢

1 个答案:

答案 0 :(得分:1)

轻松解决方案....

const badgeSchema = new mongoose.Schema ({
    badge: {
         label: { type: string, required: true}
         class: { type: string, required: true}
     }
    ..............
  })

const navItem = new Schema ({
    "title": { type: string, unique: true, required: "A Title is req."},
    .............
    "badge": { type: badgeSchema, required: false}
    ..............
  })