Map/Reduce on an array of hashes in CouchDB
- by sebastiangeiger
Hello everyone,
I am looking for a map/reduce function to calculate the status in a Design Document.
Below you can see an example document from my current database.
{
"_id": "0238f1414f2f95a47266ca43709a6591",
"_rev": "22-24a741981b4de71f33cc70c7e5744442",
"status": "retrieved image urls",
"term": "Lucas Winter",
"urls": [
{
"status": "retrieved",
"url": "http://...."
},
{
"status": "retrieved",
"url": "http://..."
}
],
"search_depth": 1,
"possible_labels": {
"gender": "male"
},
"couchrest-type": "SearchTerm"
}
I'd like to get rid of the status key and rather calculate it from the statuses of the urls.
My current by_status view looks like the following:
function(doc) {
if (doc['status']) {
emit(doc['status'], null);
}
}
I tried some things but nothing actually works. Right now my Map Function looks like this:
function(doc) {
if(doc.urls){
emit(doc._id, doc.urls)
}
}
And my Reduce Function
function(key, value, rereduce){
var reduced_status = "retrieved"
for(var url in value){
if(url.status=="new"){
reduced_status = "new";
}
}
return reduced_status;
}
The result is that I get retrieved everywhere which is definitely not right.
I tried to narrow down the problem and it seems to be that value is no array, when I use the following Reduce Function I get length 1 everywhere, which is impossible because I have 12 documents in my database, each containing between 20 to 200 urls
function(key, value, rereduce){
return value.length;
}
What am I doing wrong? (I know I want you to write code for me and I'm feeling guilty, but right now I do the calculation of the statuses in ruby after getting the data from the database. It would be nice to already get the right data from the database)