我正在开发一种带猫鼬的餐厅模型。
我想要一个餐馆文档有一个类别子类的数组,类别有一个dish subdoc和dish的数组也有一个部分subdoc数组。
它看起来好还是我应该在几个集合中分割数据?
使用ref到父/子集合创建多个集合。
猫鼬的例子:
const Category = new Schema({
name: String,
dishes: [{
type: Schema.Types.ObjectId,
ref: 'dish'
}]
});
const Dish = new Schema({
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],
name: String,
price: Number
});
您可以使用单向关系(仅限子级或父级),具体取决于您的逻辑。