I've been developing product catalog. All my needs fit perfectly with MongoDB but there is one problem:
There are categories with structure like these:
{
title:"Auto", // for h1 on page
id: 1, // for urls
level: 1, //
left: 2, // nested set implementation
right:10 //
}
When user goes to url like "www.example.com/category/1" I need to show html forms to filter shown goods. I'm pretty sure it is bad idea to store static definitions for every category on the server(about 300 categories, i'm using Django => 300 form models?)
So my idea is to store meta-information about each category in MongoDB. After some research I found out that I need 3-4 "html widgets" to construct any filter form that I need.
For example:
Range fields will look like this in HTML
<fieldset>
<legend>Price</legend>
<label for="price_from">from</label>
<input type="text" name="price_from">
<label for="price_to">to</label>
<input type="text" name="price_to">
</fieldset>
And its MongoDB JSON representation:
{
name:"price",
title:"Price",
type:"range", // "range" type - 2 inputs with initial values
categories:[1,2,5],// categories to which this so called "widget" relates
values:[
{from: 100},
{to: 500}
]
}
One more example:
Group of selects:
Its HTML version
<fieldset>
<legend>Size and color</legend>
<select name="size">
<option value="small">Small size</option>
<option value="medium">Medium size</option>
<option value="huge">Huge size</option>
</select>
<select name="color">
<option value="white">White color</option>
<option value="black">Black color</option>
<option value="purple">Purple color</option>
</select>
</fieldset>
And its MongoDB JSON version:
{
title:"Size and color",
type:"selectgroup",
categories:[2,4,6]
items:[
{
name:"size",
values:["small", "medium", "huge"]
},
{
name:"color",
values:["white", "black", "purple"]
}
]
}
So the main idea is : fetch all widgets from collection by category_id, parse them and print complete HTML form.
Pros
- Easy to add any new type of widget to database
- Easy to add parser for such widget to generate HTML from JSON
- Each widget can relates to many categories => no duplicates
Cons
- Collection of widgets has no less or more fixed structure of the document(actually i don't see real problem here)
- Аll values related to widget are embedded.
- Hard validation because fields are dynamic => no corresponding model on server side (right now i don't have any idea how to validate this)
My question is: does anybody know a better way of doing this? Pros of my method are cool, but cons are terrible.
Thanks for help and sorry for bad English.