I have a CouchDB database with the following documents.
{
"total_rows": 5,
"offset": 0,
"rows": [
{
"id": "3f46717d80b9c6be8b06d87ce80006c5",
"key": "one",
"value": "Value 1"
},
{
"id": "3f46717d80b9c6be8b06d87ce80015df",
"key": "one",
"value": "Value 2"
},
{
"id": "3f46717d80b9c6be8b06d87ce80031e6",
"key": "one",
"value": "Value 3"
},
{
"id": "3f46717d80b9c6be8b06d87ce8003deb",
"key": "two",
"value": "Value A"
},
{
"id": "3f46717d80b9c6be8b06d87ce80056eb",
"key": "two",
"value": "Value B"
}
]
}
And this view.
{
"_id": "_design/design",
"_rev": "3-c0efd39596cb5af84b1d33f07e1b1090",
"views": {
"new-view": {
"reduce": "function (keys, values) {n return values;n}",
"map": "function (doc) {n if (doc.group && doc.value)n emit(doc.group, doc._id);n}"
}
},
"language": "javascript"
}
I query the view with this URL to get a list grouped by key
, with the document ID of each record with that key.
$ curl 'http://user:password@localhost:5984/scratch/_design/design/_view/new-view?group=true' | jq
This is the output.
{
"rows": [
{
"key": "one",
"value": [
[
"3f46717d80b9c6be8b06d87ce80015df",
"3f46717d80b9c6be8b06d87ce80006c5"
],
[
"3f46717d80b9c6be8b06d87ce80031e6"
]
]
},
{
"key": "two",
"value": [
[
"3f46717d80b9c6be8b06d87ce80056eb",
"3f46717d80b9c6be8b06d87ce8003deb"
]
]
}
]
}
That looks pretty good, except that the value
values seem to be arrays of strings or arrays of arrays of strings more or less arbitrarily. (In a different database, the two ID in one group were each in an array of one string each, and another group had all three of its IDs in a single array.) Is this expected or am I going about it wrong?