Skip to content

Instantly share code, notes, and snippets.

@dch
Created June 25, 2012 08:33
Show Gist options
  • Save dch/2987400 to your computer and use it in GitHub Desktop.
Save dch/2987400 to your computer and use it in GitHub Desktop.
reducing doc.fields into an array using map keys
{
"all_or_nothing": true,
"docs": [
{
"_id": "doc1",
"device_id": "wub"
},
{
"_id": "doc2",
"device_id": "wub"
},
{
"_id": "doc3",
"device_id": "wib"
},
{
"_id": "doc4",
"device_id": "wib"
}
]
}
// put this in a wubble ddoc, in a view called wibble
// map
function(doc) {
if (doc.device_id) {
emit(doc.device_id, doc._id);
}
}
// reduce
function(key, array) {
var index = array.indexOf(key);
if (!index) {
array.push(key)
}
return (key, array.sort())
}
dave@akai /tmp % pie get $DB/_design/wubble/_view/wibble\?reduce\=false
GET /testy/_design/wubble/_view/wibble HTTP/1.1
Host: localhost:5984
Content-Type: application/json; charset=utf-8
Accept-Encoding: identity, deflate, compress, gzip
Accept: application/json
User-Agent: HTTPie/0.2.2dev
{}
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: CouchDB/1.3.0a- (Erlang OTP/R15B01)
ETag: "EK98LDWM1RZ5OOLNDK0I4V4FS"
Date: Mon, 25 Jun 2012 13:12:58 GMT
Content-Type: application/json
Cache-Control: must-revalidate
{
"offset": 0,
"rows": [
{
"id": "doc3",
"key": "wib",
"value": "doc3"
},
{
"id": "doc4",
"key": "wib",
"value": "doc4"
},
{
"id": "doc1",
"key": "wub",
"value": "doc1"
},
{
"id": "doc2",
"key": "wub",
"value": "doc2"
}
],
"total_rows": 4
}
dave@akai /tmp % pie get $DB/_design/wubble/_view/wibble\?group\=true
GET /testy/_design/wubble/_view/wibble HTTP/1.1
Host: localhost:5984
Content-Type: application/json; charset=utf-8
Accept-Encoding: identity, deflate, compress, gzip
Accept: application/json
User-Agent: HTTPie/0.2.2dev
{}
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: CouchDB/1.3.0a- (Erlang OTP/R15B01)
ETag: "7Q5NK7Q2R994UP0SAEDAK9G4R"
Date: Mon, 25 Jun 2012 13:14:09 GMT
Content-Type: application/json
Cache-Control: must-revalidate
{
"rows": [
{
"key": "wib",
"value": [
"doc3",
"doc4"
]
},
{
"key": "wub",
"value": [
"doc1",
"doc2"
]
}
]
}
{
"offset": 0,
"rows": [
{
"id": "doc1",
"key": "wub",
"value": "doc1"
},
{
"id": "doc2",
"key": "wub",
"value": "doc2"
},
{
"id": "doc3",
"key": "wib",
"value": "doc3"
},
{
"id": "doc4",
"key": "wib",
"value": "doc4"
}
],
"total_rows": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment