【发布时间】: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