Finding and Enumerating Document Attributes with ActiveCouch

February 03, 2009

Following on from my exploration of counting tags with CouchDB and map-reduce, I've added support to ActiveCouch for counting all uses of an attribute across a document type in your database. As a bonus, you can also retrieve all unique values for any attribute.

The API is straightforward. Call enumerate_all_[attribute_name] to get a hash of values and their counts, or find_all_[attribute_name] to get just the unique values:

>> Article.enumerate_all_tags
=> {"security"=>2, "ldap"=>1, "xen"=>1, "stories"=>3, "rails"=>13, "xeriom"=>3, "mysql"=>3, ... }

>> Article.find_all_tags
=> ["agile", "ajax", "apache", "api", "caching", "coding", ... ]

>> Article.find_all_author_ids
=> ["craig@barkingiguana.com"]

Under the hood, this builds the appropriate map-reduce views automatically. If you're curious about the implementation details, have a look at commit 1cbbe71.

Questions or thoughts? Get in touch.