我正在建立一个电子商务网站,并决定尝试使用MongoDB。我的目标是实现充分的灵活性,最终,我不会受限于销售特定产品,受制于最初组合系统的限制。
因此,灵活性的目标,我必须能够基于属性创建产品。 FX。颜色,制造,速度等。所有属性必须是可选的。用户可以创建新属性,有些是默认系统属性(不可删除)。根据属性的配置,它将与“基础”或可配置产品分层。
如我的示例所示,我在属性下存储了普通属性,在文档选项下可以找到“必需”属性。
我的产品文档能否以某种方式在简化方面得到改进?我害怕我要么过度或者做不到什么,因为我通过使用关系数据库发现我的想法“受损”。
祝你好运
(
[type] => Product
[sku] => elin/4191
[name] => Array
(
[da] => Sko - Elin
[en] => Shoes - Elin
)
[url_key] => Array
(
[da] => sko-elin
[en] => 1-744
)
[categories] => Array
(
)
[shops] => Array
(
[0] => 1
)
[images] => Array
(
[0] => test.jpg
[1] => test1.jpg
)
[options] => Array
(
[0] => Array
(
[color] => Array
(
[da] => Sort
[en] => Black
)
[size] => Array
(
[da] => Lille
[en] => Small
)
[shipping] => Array
(
[weight] => 0
[width] => 0
[height] => 0
[depth] => 0
)
[pricing] => Array
(
[price] => 899
[retail] => 0
[cost] => 333
[vat] => 25
[special] => Array
(
[price] => 0
[from] => new Date()
[to] => new Date()
[pct_savings] => 100
[savings] => 0
)
)
)
[1] => Array
(
[color] => Array
(
[da] => Sort
[en] => Black
)
[size] => Array
(
[da] => Medium
[en] => Medium
)
[shipping] => Array
(
[weight] => 0
[width] => 0
[height] => 0
[depth] => 0
)
[pricing] => Array
(
[price] => 899
[retail] => 0
[cost] => 333
[vat] => 25
[special] => Array
(
[price] => 0
[from] => new Date()
[to] => new Date()
[pct_savings] => 100
[savings] => 0
)
)
)
[2] => Array
(
[color] => Array
(
[da] => Orange
[en] => Orange
)
[size] => Array
(
[da] => Lille
[en] => Small
)
[shipping] => Array
(
[weight] => 0
[width] => 0
[height] => 0
[depth] => 0
)
[pricing] => Array
(
[price] => 899
[retail] => 0
[cost] => 333
[vat] => 25
[special] => Array
(
[price] => 0
[from] => new Date()
[to] => new Date()
[pct_savings] => 100
[savings] => 0
)
)
)
[3] => Array
(
[color] => Array
(
[da] => Orange
[en] => Orange
)
[size] => Array
(
[da] => Medium
[en] => Medium
)
[shipping] => Array
(
[weight] => 0
[width] => 0
[height] => 0
[depth] => 0
)
[pricing] => Array
(
[price] => 899
[retail] => 0
[cost] => 333
[vat] => 25
[special] => Array
(
[price] => 0
[from] => new Date()
[to] => new Date()
[pct_savings] => 100
[savings] => 0
)
)
)
)
[attributes] => Array
(
[designer] => Array
(
[name] => Array
(
[da] => Manufacturer
[en] => Manufacturer
)
[type] => text
[visible] => 1
[required] => false
[value] => Array
(
[da] => FunnyShirts
[en] => FunnyShirts
)
)
)
)
答案 0 :(得分:1)
我认为您的设计可以正常工作,但我认为不需要将必需属性和可选属性分隔为单独的子文档。
我不了解PHP,但是当您展示时将每种颜色视为单独的产品时,您可以执行以下操作:
product = db.products.findOne({sku: "..."})
for color in product.colors:
# render the product with the color
没有必要“溢出”您的文档 - 我假设您的意思是为每个可能的属性存储空值?您的应用程序代码应该只检查文档中是否存在可选值,并根据该值做出渲染或业务逻辑决策。 MongoDB的优势在于其灵活性。并非所有文件都必须相同。应编写应用程序代码以处理没有所有可能字段的文档。