ant*_*xic 7 mongodb database-design query functions
我还没有决定数据库。我对 MySQl 有经验,并且对MongoDB很感兴趣。
让产品成为有线电视提供商的套餐:
{
name: "Virgin TV",
price: 20
},
{
name: "Sky",
price: 25
}
Run Code Online (Sandbox Code Playgroud)
每个包裹都有一些条件。的天空包例如可以是:
但Virgin 套餐的条件会有所不同。
我只能想到为每个不同的条件创建类似存储过程的东西?
如果我不必标准化所有条件以适应数据库模式,那就太好了,因为新条件很容易来来去去。
编辑: 我已经意识到 2 个要点
现在,我们的 Sky 包装及其状况可以轻松存储:
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);
}
})
Run Code Online (Sandbox Code Playgroud)
然后,如果我们使用mapReduce,我们可以像这样查询:
db.packages.mapReduce(
function(){
emit(this.name, {...some fields..., annual_price: this.cond(this, user_input)});
},
function(key, values) {
return values;
},
{out: "tempCollection"}
).find()
Run Code Online (Sandbox Code Playgroud)
我不满意 mapReduce 输出是一个对象,它将转换后的值存储在一个名为"value". 除了 MapReduce 之外,还有其他方法可以转换集合吗?
-------过时------
MongoDB 具有服务器端函数,我认为其定义可以是这样的:
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);
}});
Run Code Online (Sandbox Code Playgroud)
对于上面的 Sky 示例。
是的,理论上看起来不错,但是如何在查询中使用它?
-------过时的结束------
欢迎对 1) 或 2) 提出任何想法。我花了相当多的时间,但我无法弄清楚。
查看第 3 章
Silverston的数据模型资源手册第 1 卷
它有一个非常灵活的定价模型
一个非常简短的总结:
有点复杂...
(*) 表示该列引用另一个表
PriceComponent
id
fromDate
thruDate null
price null
percent null
geographicBoundary* [pizza costs more outside of city]
partyType* [surcharge to seniors]
productCategory* [discount for CRT monitors]
quantityBreak* [discount for high volume]
orderValue* [discount for big order]
saleType* [surcharge for retail]
currency* [discount for Canadians]
productFeature*
product*
BasePrice : PriceComponent
DiscountComponent : PriceComponent
SurchargeComponent : PriceComponnent
ManufacturersSuggestedRetailPrice : PriceComponent
Charge : PriceComponent
OneTimeCharge : Charge
RecurringCharge : Charge
UtilizationCharge : Charge
Run Code Online (Sandbox Code Playgroud)
因此,某物的价格是一组 PriceComponents,可以是实际价格金额或百分比。您可以根据地理位置、销售类型(零售与批发)、订单价值等确定最终价格的组成部分。