值得在mongoose模型中拥有许多子文档

问题描述 投票:0回答:1

我正在开发一种带猫鼬的餐厅模型。

我想要一个餐馆文档有一个类别子类的数组,类别有一个dish subdoc和dish的数组也有一个部分subdoc数组。

它看起来好还是我应该在几个集合中分割数据?

javascript mongodb mongoose
1个回答
0
投票

使用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
});

您可以使用单向关系(仅限子级或父级),具体取决于您的逻辑。

http://mongoosejs.com/docs/populate.html

© www.soinside.com 2019 - 2024. All rights reserved.