繁体   English   中英

mongoDB中每条记录的字段之间的多对多数据建模

[英]Many-to-Many data modelling between fields for each record in mongoDB

让我们说我必须保存mongoDB中的布料记录。 布料的属性是

  • 名称
  • 描述
  • 样式
    • 尺寸
    • 颜色
    • 条件
    • 品牌
    • someAttrubute
  • 价钱

对于每种布料价格的变化,每种风格和品牌的组合。 那么我如何在mongoDB中对此进行建模。 到目前为止,我一直在想的是:

{
  "name": "A name",
  "description": "A typical description",
  "style":[
    {"size": "XL","color": "red", "condition": "good"},//--style 0
    {"size": "XXL","color": "white", "condition": "bad"},//--style 1
    //...
    {"size": "L","color": "black", "condition": "best"}//--style N
  ],
  "brand":[
    {"brandName":"brand0","someAttribute":"Attribute 0"},
    {"brandName":"brand1","someAttribute":"Attribute 1"},
    {"brandName":"brand2","someAttribute":"Attribute 2"}
  ],
  "price":[
    //Every price need to be added for every combination of brand and style
    {"style":0,"brand":0,"price": 10},
    {"style":0,"brand":1,"price": 20},
    {"style":0,"brand":2,"price": 30},
    {"style":1,"brand":0,"price": 10},
    {"style":1,"brand":1,"price": 20},
    //...
    {"style":"N","brand":2,"price": 10}
  ]
}

我不认为这是在mongoDB中执行此操作的正确方法。 如何建模?

我会这样的,

{
    "name": "A name",
    "description": "A typical description",
    "priceGroup" : [
        {
            "style": {"size": "XL","color": "red", "condition": "good"},
            "brand": {"brandName":"brand0","someAttribute":"Attribute 0"}
            "price": 10 
        },
        {
            "style": {"size": "XXL","color": "white", "condition": "bad"},
            "brand": {"brandName":"brand0","someAttribute":"Attribute 0"}
            "price": 20 
        },
        {
            "style": {"size": "XL","color": "red", "condition": "good"},
            "brand": {"brandName":"brand1","someAttribute":"Attribute 1"}
            "price": 30 
        },
        {
            "style": {"size": "XXL","color": "white", "condition": "bad"},
            "brand": {"brandName":"brand1","someAttribute":"Attribute 1"}
            "price": 40 
        },
        .....
    ]
}

但正如@Neil Lunn指出的那样,在设计nosql模式时,关系数据库设计概念中没有规则 - 没有规范化。 因此,更多的是您的应用和要求。 将您要查询的内容放在一个集合中,将其他内容放在一个不同的集合中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM