Reputation: 1363
I’m building an ecommerce site, and have decided to try out MongoDB. My goal is to achieve full flexibility, so that in the end, I won’t be tied to selling specific products, limited by how the system was originally put together.
Hence the goal of flexibility, I must be able to create products based on attributes. Fx. color, manufacture, speed etc. All attributes must be optional. The user can create new attributes, and some are default system attributes (non deletable). Based on the configuration of the attribute, it will be layered with either the “base”- or configurable product.
As seen in my example, I have normal attributes stored under attributes, and “required” attributes to be found under the document options.
Can my product document somehow be improved in terms of making things easier? I’m afraid that I’m either over- or underdoing something, since I find my ideas “damaged” by working with relational databases.
Best regards
(
[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
)
)
)
)
Upvotes: 4
Views: 2022
Reputation: 24007
I think your design will work fine, although I don't see a need to separate required and optional attributes into separate subdocuments.
I don't know PHP, but to treat each color as a separate product when you display, you could do something like:
product = db.products.findOne({sku: "..."})
for color in product.colors:
# render the product with the color
There's no need to "overfill" your document -- I assume you mean to store null values for every possible attribute? Your application code should simply check whether an optional value is present in the document and make its rendering or business-logic decisions based on that. Among MongoDB's strengths is its flexibility. Not all documents have to be the same. Your application code should be written to handle documents which don't have all possible fields.
Upvotes: 1