incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anand Chitipothu <anandol...@gmail.com>
Subject Re: how to count the number of unique values
Date Tue, 12 Oct 2010 15:00:53 GMT
2010/10/12 Michael Zedeler <michael@zedeler.dk>:
>  On 2010-10-12 15:09, Anand Chitipothu wrote:
>>
>> Is it possible to count the number of unique values by writing a couchdb
>> view?
>>
>> Consider the following 2 docs.
>>
>> {
>>     "_id": "posts/1",
>>     "title": "post 1",
>>     "tags": ["foo", "bar"]
>> }
>>
>> {
>>     "_id": "posts/2",
>>     "title": "post 2",
>>     "tags": ["foo", "foobar"]
>> }
>>
>> Is it possible to find that there are 3 tags?
>
> Yes. Just write a map function that emits all tags it finds (not checked and
> probably wrong):
>
> function(doc) {
>    for(tag in doc.tags) {
>        emit(tag, null);
>    }
> }
>
> In the reduce-function, just use _count
> (http://wiki.apache.org/couchdb/Built-In_Reduce_Functions).

That gives counts of each tag, not the total number of unique tags.

The above reduce function with group_level=1 will give:

{"rows":[
{"key":"bar","value":1},
{"key":"foo","value":2},
{"key":"foobar","value":1}
]}

And without any group_level it will return 4, which is the total
number of tags occurrences.

{"rows":[
{"key":"null","value":4}
]}

Is there any way to find the number of *unique* tags?

Anand

Mime
View raw message