It's not quite clear exactly what you want. I see that you are making your value list unique, so I'm assuming that you want some sort of unique or last function?

I notice a mistake in your code: when creating a reduce function, a re- reduce should behave in the same way as the regular reduce. The reason is that CouchDB doesn't necessarily call re-reduce on your map results.

Think about it this way: If you have a bunch of values V1 V2 V3 for key K, then you can get the combined result either by calling reduce([K,K,K],[V1,V2,V3],0) or by re-reducing the individual results: reduce(null,[R1,R2,R3],1). This depends on what your view results look like internally.


That said, what are you interested in? Do you want the last-changed title or do you want all titles in order of appearance?

If the latter, you don't need a reduce at all, you just look at the results of your map function in order.

If the former, you'll have to put your date in the value of the map, and in the reduce you'll have to decide which title you want to keep for that set of values.

Wout.

PS: I changed the wiki so that it has a common mistakes section in the ViewSnippets page and I added the reduce mistake.

On Apr 18, 2009, at 6:52 PM, Nicolas Clairon wrote:

Hi all !

I have a question (wich is a big concerne to me and my project) about
the group_level option.

I want to display all doc by tag and then sorting them by date.

The map function :
-----------------------------------
function(doc){
 for(var t in doc.tags){
   emit([doc.tags[t], doc.creation_date], doc.title);
 }
}
------------------------------------

* creation_date is a float since the epoch (ie something like this 12423344.003)
* docs can have the same title

the reduce function:
-----------------------------------
function(key, values, rereduce){
   var results = [];
   if(!rereduce){
       for( var v in values){
if (results.indexOf( values[v] ) < 0) {results.push(values[v]);}
       }
   }
   else{
       for( var i in values){
           for(var e in values[i]){
               results.push(values[i][e]);
           }
       }
   }
   return results;
}
-----------------------------------

$ curl 
http://localhost:5984/db/_design/foo/_view/by_tag_sort_by_date?reduce=false

returns :

{"id":"8075ba2ef7418f4d6c9a3e89be83acd8","key":["tag1", 1239361935.000004],"value":"title2"}, {"id":"8d9132318a6c34c646e9e2cd43823ffa","key":["tag1", 1239794744.000002],"value":"title1"}, {"id":"f49a28ffd2118298c1be7440ec4556fa","key":["tag2", 1239794744.000002],"value":"title1"},

this is ok because title1 is newer than title2. But now, I want all
displayed by tag so I use the group_level :

$ curl 
http://localhost:5984/db/_design/foo/_view/by_tag_sort_by_date?group_level=1

{"key":["tag1"],"value":["title1","title2"]},
{"key":["tag2"],"value":["title1"]},

I have all titles by tag but the docs is not sorted by date anymore...

Does group_level keeps the absolute sorting ? Does the sort break anyway ?

Thanks,

Nicolas

Reply via email to