我正在尝试汇总我在 CouchDB 中的一些数据。数据库中有照片文件,数据库中有评级文件。每个评级文档看起来像这样(没有 _id 和 _rev):
{
"type": "rating",
"rating": 3 // Integer values are identifiers that map to a string; e.g 1 might mean 'funny'
"photo": "as9i83ufsafa09uj" // The id of the photo that this rating points to.
}
我想要做的是计算每张照片的每种评级类型。
{
"key": "as9i83ufsafa09uj", "value": [1, 7, 8, 6] // 1 '0' rating, 7 '1' ratings, etc.
"key": "photoid2", "value": [3, 0, 0, 8]
}
我正在使用 CouchDB 视图的 MapReduce 来实现这种聚合。
"map": "function(doc) {
if(doc.type == 'rating')
{
emit(doc.photo, doc.rating);
}
}",
"reduce": "function(keys, values, rereduce) {
var result = new Array(0, 0, 0, 0);
values.forEach( function(key, value)
{
result[value]+=1;
});
return result;
}"
地图返回:
{"total_rows":55,"offset":0,"rows":[
{"id":"0aa2c4c9a031eedbcf2795cabc1679be","key":"4aa5ec26-26b8-490a-a9cc-620a0d2136b9","value":0},
{"id":"29f363432e008f5934b4160292e18680","key":"4aa5ec26-26b8-490a-a9cc-620a0d2136b9","value":3},
{"id":"646d0d764623bc2f3ed1354ac03b583e","key":"4aa5ec26-26b8-490a-a9cc-620a0d2136b9","value":2},
...
{"id":"fa5be78402171e3bf1eb1cf91c5fda6e","key":"c63b78b6-ad92-426c-ab64-c9a6ae229b31","value":1}
]}
在 group_level=0 的情况下,reduce 返回:
{"rows":[
{"key":null,"value":[1,1,1,1]}
]}
组级别=1:
{"rows":[
{"key":"4aa5ec26-26b8-490a-a9cc-620a0d2136b9","value":[2,2,2,0]},
{"key":"5ad3de4b-d25b-42d3-95e0-df7661becbf3","value":[2,2,2,2]},
{"key":"7600710b-9ae3-4312-876c-ad352722dac3","value":[2,2,2,2]},
{"key":"959f48a2-5018-4938-aab4-086d8824dd75","value":[2,0,0,0]},
{"key":"c63b78b6-ad92-426c-ab64-c9a6ae229b31","value":[2,2,2,2]}
]}
我熟悉 MongoDB 的 map reduce,这个函数可以使用他们的模式。我需要调整什么才能在 CouchDB 中工作?
更新 这是对我有用的最后一个 reduce 函数。我没有正确处理 rereduce 参数。感谢 Marcin Skórzewski 帮助我更好地理解 rereduce。
"reduce": "function(key, values, rereduce) {
var result = new Array(0, 0, 0, 0);
if(rereduce == true)
{
for(var i = 0; i < values.length; i++)
{
var value = values[i];
for (var j = 0; j < value.length; j++)
{
result[j] += value[j];
}
}
return result;
}
for(var i = 0; i < values.length; i++)
{
value = values[i];
result[value]+=1;
}
return result;
}"