I haven't decided on a database. I have expirience with MySQl and I'm intrigued by MongoDB.
Let the products be packages of cable tv providers:
{
name: "Virgin TV",
price: 20
},
{
name: "Sky",
price: 25
}
Every package comes with some conditions. The Sky package for example may be:
- The first three months the price is XXX then it becomes YYY.
- If you watch TV after 00:00 is XXX-10.
- If you are the N'th customer you have 10% discount.
- ...
But the conditions for the Virgin package will be different.
1) How do I actually store the products along with all their conditions?
I could only think of creating something like stored procedure for every different condition?
It would be nice if I don't have to normalize all conditions to fit a database schema because new conditions can easily come and go.
EDIT : I've realized 2 essentials
- MongoDB supports anonymous functions as a value of a field.
- Map/Reduce can transform a collection to another using a function
Now our the Sky package along with its condition can be easily stored:
db.packages.insert({
name: "Sky"
price: 25,
condition: function(object, user_input){
time_discount = user_input.time > 0 and user_input.time < 6 ? 10 : 0;
price = price - time_discount;
return 6*price + 6*(price*1.2);
}
})
Then if we use mapReduce we can query in like:
db.packages.mapReduce(
function(){
emit(this.name, {...some fields..., annual_price: this.cond(this, user_input)});
},
function(key, values) {
return values;
},
{out: "tempCollection"}
).find()
I'm not happy with the fact the mapReduce output is a an object which stores the transformed values in a single key named "value"
. Is there another way to transform a collection other than MapReduce?
------- Outdated ------
MongoDB has server-side functions which definition I presume can say something like:
db.system.js.save( { _id : "sky_annual_price" , value : function(price, user_input){
time_discount = user_input.time > 0 and user_input.time < 6 ? 10 : 0;
price = price - time_discount;
return 6*price + 6*(price*1.2);
}});
for the Sky example above.
Yes, in theory looks nice but how can this be used in a query?
------- End of outdated ------
2) How do I query such information so that I can sort by the annual price ?
Any ideas on both 1) or 2) are welcome. I've spent considerable amount of time but I can't figure that out.