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

时间:2016-04-21 06:16:34

标签: 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中执行此操作的正确方法。如何建模?

1 个答案:

答案 0 :(得分:0)

我会这样,

{
    "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模式时,关系数据库设计概念中没有规则 - 没有规范化。因此,更多的是您的应用和要求。将您将要查询的内容放在一个集合中,将其他内容放在一个不同的集合中。