让我们说我必须保存mongoDB中的布料记录。布料的属性是
每种布料价格的变化,每种风格和品牌的组合。那么我如何在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中执行此操作的正确方法。如何建模?
答案 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模式时,关系数据库设计概念中没有规则 - 没有规范化。因此,更多的是您的应用和要求。将您将要查询的内容放在一个集合中,将其他内容放在一个不同的集合中。