【发布时间】:2019-02-07 20:13:26
【问题描述】:
我有一个用于产品类别 (MongoDB) 的 keystonejs 模型。有些类别应该有子类别。目前我已经设置了一个关系字段“ChildCategoryOf”,我可以在管理面板中手动选择父类别。为了拥有更多功能,我想创建另一个名为“ParentCategoryOf”的字段,该字段将包含一个子类别数组。怎么可能有一个字段自动将子类别存储在数组中?我想象它是这样的:
当前型号:
let ProductCategory = new keystone.List('ProductCategory', {
autokey: {
from: 'name',
path: 'key',
unique: true
}
});
ProductCategory.add({
name: {
type: String,
required: true
},
ChildCategoryOf: {
type: Types.Relationship,
ref: 'ProductCategory',
many: false,
required: false,
},
IsParentCategory: Types.Boolean,
});
【问题讨论】:
-
对于 1xN 关系,通常您不会让父类明确持有对子类的引用。如果您想获取所有孩子,通常会在数据库中查询具有该父 ID 的所有孩子。
标签: mongodb keystonejs