Mongodb 子类别的模式设计
作为一个例子,我想有食物类别,子类别和实际的菜单项目本身。所有菜单项都必须有一个类别,但并非所有菜单项都必须有一个子类别 比如说: 仅类别示例:Mongodb 子类别的模式设计,mongodb,mongoose,database-design,Mongodb,Mongoose,Database Design,作为一个例子,我想有食物类别,子类别和实际的菜单项目本身。所有菜单项都必须有一个类别,但并非所有菜单项都必须有一个子类别 比如说: 仅类别示例: Hamburgers - Bacon and cheese - Chicken 类别和子类别示例: Pizza Traditional - Cheese and mushroom - Tomato and ham Deluxe - Bacon, ham, salami, st
Hamburgers
- Bacon and cheese
- Chicken
类别和子类别示例:
Pizza
Traditional
- Cheese and mushroom
- Tomato and ham
Deluxe
- Bacon, ham, salami, steak
这意味着子类别是可选的。因此,在构建表单以添加菜单项时,是否应该在子类别上设置“无”选项,或者如果从下拉菜单中未选择任何选项,则可以在数据库中插入空字符串
这就是我的模式,但不确定它是否适合我的需要
const menuSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 5,
maxlenth: 255
},
description: {
type: String,
required: true,
minlength: 5,
maxlength: 255
},
price: {
type: Number,
required: true,
min: 0
},
category: {
type: categorySchema,
required: true
},
subcategory: {
type: subcategorySchema
}
});
const Menu = mongoose.model("Menu", menuSchema);
category和subcategory模式基本相同,但是category和subcategory有两个单独的模式更好,还是只有一个包含嵌入式子类别的categories模式更好,或者最好的方法是什么
const categorySchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 5,
maxlength: 255
}
});
const Category = mongoose.model("Category", categorySchema);