couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Wang <javap...@gmail.com>
Subject Re: map/reduce function question
Date Tue, 28 Apr 2009 06:06:21 GMT
Hi Paul,

Thanks much for your kind help!

Will this only return the number of occurrences for each unique name, rather
than the number of unique names?

When I used Futon to access that view, it returned a list of all unique
names with corresponding number of occurrences for each of them.

When I used command curl, the result is:

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

Which seems to be the total number of docs.

Am I stupid or do I miss something?

Thanks!

-- Kevin

On Mon, Apr 27, 2009 at 10:42 PM, Paul Davis <paul.joseph.davis@gmail.com>wrote:

> Kevin,
>
> You'll want to do something like this:
>
> {
>   "_id": "_design/foo",
>   "views": {
>       "unique_names": {
>           "map": "function(doc) {emit(doc.name, 1);}",
>           "reduce": function(keys, values, rereduce) {return sum(values);}"
>      }
>   }
> }
>
> And then you can call this like:
>
>   http://127.0.0.1:5984/db_name/_design/foo/_view/unique_names
>
> to get ouput like
>
> {"rows": [
>   {"key": null, "value": 3}
> }
>
> HTH,
> Paul Davis
>
>
> Kevin Wang wrote:
>
>> Hi All,
>>
>> While I am trying to switch my mind to map/reduce model while accessing
>> couchdb data, I found myself struggling.  Here is a simple question that
>> you
>> guys might be able to answer:
>>
>> If I have the following set of data in a doc:
>>
>> {"name": "foo", "prop": "value1"}
>> {"name": "bar", "prop": "value2"}
>> {"name": "foo", "prop": "value3"}
>> {"name": "bob", "prop": "value4"}
>>
>> How to write the map/reduce functions so that I can get a view which
>> returns
>> the total number of unique name's?  In this case, the result should be 3.
>>
>> Thanks!
>>
>> -- Kevin
>>
>>
>>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message