Couchdb中的Map Reduce函数将相同键的值分组

时间:2019-05-11 04:31:26

标签: couchdb

我有以下格式的ouchdb文件:

{
  "_id": "ac4a1c9ec5921b8537fcfe4e450046ab",
  "_rev": "4-4911f4615bcefa6dee88068bc8524930",
  "text": "hello how are you",
  "code": 111
}

{
  "_id": "ac4a1c9ec5921b8537fcfe4e450057ee",
  "_rev": "3-f2c94a551065000ba7d468a5a7b2624a",
  "text": "what is your age",
  "code": 112
}

{
  "_id": "ac4a1c9ec5921b8537fcfe4e45010d36",
  "_rev": "1-9c1b7d02eb0497ca3696f2ccda7df2de",
  "text": "keep it up",
  "code": 112
}

我想写一个map reduce以便我可以根据代码对文本字段进行分组

输出应如下所示:

key   Text
111   [hello how are you]
112   [what is your age , keep it up]

1 个答案:

答案 0 :(得分:1)

这很容易实现,您可以使用以下代码。

视图:

function (doc) {
  emit(doc.code, doc.text);
}

减少

function (keys, values, rereduce) {
  return values.join(",")
}

结果

{"rows":[
{"key":100,"value":"one hundred"},
{"key":123,"value":"hello test,hello world"}
]}

我在Cloudant上进行了测试。让我知道CouchDB是否存在问题