我的CouchDB数据库中有以下文档: -
{
"_id": "c0d93b8f424037583b2fe71ea403044a",
"name": "Joe",
"country": "United Kingdom",
"email": "joe@acme.com"
}
{
"_id": "c0d93b8f424037583b2fe71ea403044d",
"name": "Joe",
"country": "United Kingdom",
"email": "joe@acme.com"
}
{
"_id": "c0d93b8f424037583b2fe71ea403044z",
"name": "Joe",
"country": "United Kingdom",
"email": "joe@acme.com"
}
{
"_id": "c0d93b8f424037583b2fe71ea403044x",
"name": "Fred",
"country": "France",
"email": "Fred@acme.com"
}
{
"_id": "c0d93b8f424037583b2fe71ea403044g",
"name": "Fred",
"country": "France",
"email": "Fred@acme.com"
}
{
"_id": "c0d93b8f424037583b2fe71ea403044p",
"name": "Bill",
"country": "Germany",
"email": "Bill@acme.com"
}
我想总结这些数据,以便我可以根据国家/地区生成饼图,但我正在努力制定地图缩减代码以按国家/地区获取文档计数。
请有人为我解释一下。
最诚挚的问候,
Carlskii
答案 0 :(得分:1)
这很简单。
您的地图功能只会将国家/地区作为关键字, 1 作为值。
function(doc) {
if (doc.country) {
emit(doc.country, 1);
}
}
现在,您的reduce函数只是built-in _sum
函数。
使用group_level
参数将其设置为1来查询您的视图。
GET / db / _design / app / _view / bycountry?group_level = 1`
您的结果应如下所示。
{
"rows": [
{"key": "France", "value": 2},
{"key": "Germany", "value": 1},
{"key": "United Kingdom", "value": 3}
]
}