couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Clairon <clai...@gmail.com>
Subject Re: Random issue with reduce
Date Sat, 28 Mar 2009 11:54:02 GMT
I find a work around by using rereduce like this :

    function( key, values, rereduce){
        var results = [];
        if (!rereduce) {
            for (var i in values){
                if (results.indexOf(values[i]) == -1){results.push(
values[i] );}
            }
            return results;
        }
        else {
            var results = [];
            for( var i in values){
                for(var e in values[i]){
                    results.push(values[i][e]);
                }
            }
            return results;
        }
    }

But I don't understand why sometime I need to rereduce and sometime
(for the same
expected result) it is not necessary

This is the first version of the reduce function wich produce a result
with nested lists...

    function( key, values, rereduce){
        var results = [];
        for (var i in values){
             if (results.indexOf(values[i]) == -1){results.push( values[i] );}
        }
        return results;
     }

If someone has an anwser...

On Sat, Mar 28, 2009 at 12:25 PM, Nicolas Clairon <clairon@gmail.com> wrote:
> I use this form of reduce to group by key the results.
> In the real life, I use this reduce to get all tag group by document id.
>
> Something like that :
>
> function(doc){
>   if(doc.doc_type == "MyDoc"){
>     for( var t in doc.tags){
>       emit(doc.name, doc.tags[t]);
>     }
>   }
> }
>
> and the reduce :
>
> function(key, values){
>  return values;
> }
>
> I get the following result:
>
> name1 = > ["tag1", "tag2", "tag3"]
> name2 => ["tag2", "tag3"]
>
> but sometime I get this result
>
> name1 = [["tag1","tag2","tag3"],["tag4","tag5","tag6","tag7"],["tag8","tag9"]]
>
> Note that if the view work well the first time, it will always work fine.
>
> It is the correct way to go ?
>
> On Sat, Mar 28, 2009 at 12:10 PM, Sven Helmberger
> <sven.helmberger@gmx.de> wrote:
>> Nicolas Clairon schrieb:
>>>
>>> Nop !
>>>
>>> Here is the test  map function :
>>>
>>> function(doc){
>>>  emit("key", 1);
>>> }
>>>
>>> and the reduce function:
>>>
>>> function(key, values){
>>>  return values;
>>> }
>>>
>>> That work well for a large usecase but sometime, I get this strange
>>> behavior...
>>>
>>
>> What purpose does the reduce function have? it doesn't really reduce or
>> combine anything so it seems like it could just as well be left out.
>>
>> Regards,
>> Sven Helmberger
>>
>

Mime
View raw message