猫鼬模式 - 需要 Children,而不是 Parent

Mongoose Schema - Required Children, not Parent

我的导航架构中有以下 object:

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

虽然徽章不是必需的,但标签和 class 如果确实存在徽章。

如何在我的 mongoose 架构中正确显示对可选徽章的要求?

谢谢

简单的解决方案....

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}
    ..............
  })