Hardik
Hardik

Reputation: 11

Map Reduce function in couchdb to group the values of same key

I have my couchdb documents in the following form :

{
  "_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
}

i want write a map reduce so that i can group the text field on basis of code

The output should look like:

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

Upvotes: 1

Views: 52

Answers (1)

Joshua Beckers
Joshua Beckers

Reputation: 907

This is quiet easy to do, you can use the following code.

The view:

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

The reduce

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

The result

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

I tested it on Cloudant. Let me know if there is an issue on CouchDB

Upvotes: 1

Related Questions