You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Alex Brandt <al...@alunduil.com> on 2016/01/16 05:49:14 UTC

Categorization Views

Hey,

I've been poking at how to construct views using the map reduce mechanics but 
am struggling with crafting a particular view.  I have a set of data where 
documents have a category and I'd like to have a view that provides me with 
groups of documents based on their category.  Is it possible to get the 
following three documents into the subsequent view (loosely defined)?

* { "category": "A", "_id": "1" }
* { "category": "A", "_id": "2" }
* { "category": "B", "_id": "3" }

View:

{ "A": [ { "category": "A", "_id": "1" }, { "category": "A", "_id": "2" } ], 
"B": [ { "category": "B", "_id": "3" } ] }

Any pointers would be appreciated.

Thanks,

-- 
Alex Brandt
Software Developer for Rackspace and Developer for Gentoo
http://blog.alunduil.com

Re: Categorization Views

Posted by Ingo Radatz <th...@googlemail.com>.
Hi Alex!

> I've been poking at how to construct views using the map reduce mechanics but 
> am struggling with crafting a particular view.  I have a set of data where 
> documents have a category and I'd like to have a view that provides me with 
> groups of documents based on their category.  Is it possible to get the 
> following three documents into the subsequent view (loosely defined)?
> 
> * { "category": "A", "_id": "1" }
> * { "category": "A", "_id": "2" }
> * { "category": "B", "_id": "3" }
> 
> View:
> 
> { "A": [ { "category": "A", "_id": "1" }, { "category": "A", "_id": "2" } ], 
> "B": [ { "category": "B", "_id": "3" } ] }

Don’t try to group category members in the “value” of a view row. Just emit a row per doc with the category as key and request the key:

====

function () {
  emit(doc.category, null)     //the doc._id is automatically included
}

====

GET db/_design/:ddocname/_view/:viewname?key=‘A’

====

{
  rows: [
    {id: “1”, key: “A”, value: null},
    {id: “2”, key: “A”, value: null}
  ]
}


Best, ingo