【问题标题】:Mongoose Schema - Required Children, not ParentMongoose Schema - 必需的子项,而不是父项
【发布时间】:2016-11-08 05:02:08
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: mongoose mongoose-schema


    【解决方案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}
        ..............
      })
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多