2010/10/12 Michael Zedeler <[email protected]>:
> 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