【问题标题】:Mongodb: which datatype must be used in Mongoose schema to store null and ObjectId of the documentMongodb:Mongoose模式中必须使用哪种数据类型来存储文档的null和ObjectId
【发布时间】:2021-02-15 23:35:42
【问题描述】:

我用于类别集合的架构如下所示:

{
  name:{ type: String, required: true},
  parent: { type: mongoose.Schema.Types.ObjectId, required: true}
}

现在,对于所有级别 1 类别,我想将父级存储为“null”,而级别 2 等将存储父级文档的 ObjectId。如何在模式中定义此条件?我不能使用 String 类型,因为它不允许我在需要时映射到父文档。任何其他解决方案可以实现这一目标?

【问题讨论】:

    标签: mongodb mongoose mongoose-schema


    【解决方案1】:
    {
      name:{ type: String, required: true},
      parent: { type: mongoose.Schema.Types.ObjectId, required: true, default: null}
    }
    

    将默认键设置为 null 可能会有所帮助...如果未插入值,则默认为 null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-30
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      相关资源
      最近更新 更多