猫鼬-带有子文档的架构设计

时间:2020-07-24 12:31:14

标签: javascript mongodb mongoose mongoose-schema subdocument

我正在为一家多城市的鞋业公司(例如耐克)建立一个中央数据库 在处理价格和库存时确实陷入了模式设计。

我在想以下几点:

  1. 产品架构(例如,跑鞋)-包含有关产品的一些元数据,并具有一个品种数组(子文档数组),该数组具有实际的鞋,例如“跑鞋1”,“跑鞋2”等...
const productSchema = new mongoose.Schema({
    title: {type: String, required: true},
    category: {type: String, required: true},
    backgroundColor: {type: String, default: 'white'},
    imgSrc: {type: String, required: true},
    varieties: {
        type: [varietySchema],
    },
});

const varietySchema = new mongoose.Schema({
    title: {type: String, required: true},
    imgSrc: {type: String, required: true},
    backgroundColor: {type: String, default: 'white'},
});

mongoose.model('Product', productSchema);
  1. 商店架构(例如,纽约市)-包括产品参考列表以及带有各种参考以及价格和库存的各种阵列
const storeSchema = new mongoose.Schema({
    title: {type: String, required: true},
    inventory: [{
        productRef: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Product'
        },
        varieties: [{
            varietyRef: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'Product.varieties'
            },
            stock: Number,
            price: Number
        }]
    }]
});

我强烈认为这不是设计方案的最佳解决方案,但我想不出一种满足我要求的解决方法。同样,在商店模型中创建和更新条目似乎本身就是一项艰巨的任务,因为每次更新时我都必须检查产品和品种的存在,并且在商店库存中缺少两者或其中任何一个时都需要备用处理程序。有人可以帮我吗? :)

0 个答案:

没有答案