考虑下面存储在CouchDB中的示例文档
{
"_id":....,
"rev":....,
"type":"orders",
"Period":"2013-01",
"Region":"East",
"Category":"Stationary",
"Product":"Pen",
"Rate":1,
"Qty":10,
"Amount":10
}
{
"_id":....,
"rev":....,
"type":"orders",
"Period":"2013-02",
"Region":"South",
"Category":"Food",
"Product":"Biscuit",
"Rate":7,
"Qty":5,
"Amount":35
}
Run Code Online (Sandbox Code Playgroud)
考虑以下SQL查询
SELECT Period, Region,Category, Product, Min(Rate),Max(Rate),Count(Rate), Sum(Qty),Sum(Amount)
FROM Sales
GROUP BY Period,Region,Category, Product;
Run Code Online (Sandbox Code Playgroud)
是否可以在couchdb中创建与上述SQL查询等效的map/reduce视图,并生成类似的输出
[
{
"Period":"2013-01",
"Region":"East",
"Category":"Stationary",
"Product":"Pen",
"MinRate":1,
"MaxRate":2,
"OrdersCount":20,
"TotQty":1000,
"Amount":1750
},
{
...
}
]
Run Code Online (Sandbox Code Playgroud)
首先,我相信@benedolph 的答案是最佳实践和最佳案例场景。理想情况下,每个归约应返回 1 个标量值,以使代码尽可能简单。
但是,您确实必须发出多个查询才能检索问题描述的完整结果集。如果您无法选择并行运行查询,或者减少查询数量非常重要,则可以一次完成所有操作。
您的地图功能将非常简单:
function (doc) {
emit([ doc.Period, doc.Region, doc.Category, doc.Product ], doc);
}
Run Code Online (Sandbox Code Playgroud)
减少函数是它变得冗长的地方:
function (key, values, rereduce) {
// helper function to sum all the values of a specified field in an array of objects
function sumField(arr, field) {
return arr.reduce(function (prev, cur) {
return prev + cur[field];
}, 0);
}
// helper function to create an array of just a single property from an array of objects
// (this function came from underscore.js, at least it's name and concept)
function pluck(arr, field) {
return arr.map(function (item) {
return item[field];
});
}
// rereduce made this more challenging, and I could not thoroughly test this right now
// see the CouchDB wiki for more information
if (rereduce) {
// a rereduce handles transitionary values
// (so the "values" below are the results of previous reduce functions, not the map function)
return {
OrdersCount: sumField(values, "OrdersCount"),
MinRate: Math.min.apply(Math, pluck(values, "MinRate")),
MaxRate: Math.max.apply(Math, pluck(values, "MaxRate")),
TotQty: sumField(values, "TotQty"),
Amount: sumField(values, "Amount")
};
} else {
var rates = pluck(values, "Rate");
// This takes a group of documents and gives you the stats you were asking for
return {
OrdersCount: values.length,
MinRate: Math.min.apply(Math, rates),
MaxRate: Math.max.apply(Math, rates),
TotQty: sumField(values, "Qty"),
Amount: sumField(values, "Amount")
};
}
}
Run Code Online (Sandbox Code Playgroud)
我根本无法测试这段代码的“rereduce”分支,你必须自己测试。(但这应该有效)有关减少与重新减少的信息,请参阅wiki 。
我在顶部添加的辅助函数实际上使代码整体更短且更易于阅读,它们很大程度上受到我使用Underscore.js 的经验的影响。但是,您不能在reduce 函数中包含CommonJS 模块,因此必须手动编写。
同样,最好的情况是让每个聚合字段都有自己的映射/归约索引,但如果您无法选择,则上面的代码应该可以为您提供问题中所描述的内容。