在 reduce 函数上按键中断
Break by key on reduce function
我正在使用 Cloudant(基于 CouchDB)存储一些数据,并想创建一个 reduce 函数来按键汇总所有内容。我正在使用以下映射函数:
function (doc) {
emit(doc.name, 1);
}
因此,我收到了这样一个 json
文件:
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "605b21a9c295ec9c03ae2a6aadeb1422",
"key": "foo",
"value": 1
},
{
"id": "cb4258669082efeae9391288eb684339",
"key": "foo",
"value": 1
},
{
"id": "f2e7e69d6a3d26fa3b8cad1f0c63ccc3",
"key": "bar",
"value": 1
}
]
}
因此,对于 _sum
内置函数,我期待的是:
{
"rows": [
{
"key": "foo",
"value": 2
},
{
"key": "bar",
"value": 1
}
]
}
但我明白了:
{
"rows": [
{
"key": null,
"value": 3
}
]
}
我正在阅读 Cloudant 文档,发现 _sum
函数:
Produces the sum of all values for a key, values must be numeric
但结果不是 key
返回,而是我地图上的每个 value
。
您忘记设置 reduce 查询参数。
我用了同样的map函数+_sum函数。
URL : http://host:5984/db/_design/stack/_view/byname?reduce=true&group=true
结果:
{"rows":[
{"key":"bar","value":1},
{"key":"foo","value":2}
]}
我正在使用 Cloudant(基于 CouchDB)存储一些数据,并想创建一个 reduce 函数来按键汇总所有内容。我正在使用以下映射函数:
function (doc) {
emit(doc.name, 1);
}
因此,我收到了这样一个 json
文件:
{
"total_rows": 3,
"offset": 0,
"rows": [
{
"id": "605b21a9c295ec9c03ae2a6aadeb1422",
"key": "foo",
"value": 1
},
{
"id": "cb4258669082efeae9391288eb684339",
"key": "foo",
"value": 1
},
{
"id": "f2e7e69d6a3d26fa3b8cad1f0c63ccc3",
"key": "bar",
"value": 1
}
]
}
因此,对于 _sum
内置函数,我期待的是:
{
"rows": [
{
"key": "foo",
"value": 2
},
{
"key": "bar",
"value": 1
}
]
}
但我明白了:
{
"rows": [
{
"key": null,
"value": 3
}
]
}
我正在阅读 Cloudant 文档,发现 _sum
函数:
Produces the sum of all values for a key, values must be numeric
但结果不是 key
返回,而是我地图上的每个 value
。
您忘记设置 reduce 查询参数。
我用了同样的map函数+_sum函数。
URL : http://host:5984/db/_design/stack/_view/byname?reduce=true&group=true
结果:
{"rows":[
{"key":"bar","value":1},
{"key":"foo","value":2}
]}