CouchDB- basic grouping question
        Posted  
        
            by dnolen
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by dnolen
        
        
        
        Published on 2009-07-02T23:04:50Z
        Indexed on 
            2010/03/17
            10:11 UTC
        
        
        Read the original article
        Hit count: 174
        
I have a user document which has a group field. This field is an array of group ids. I would like to write a view that returns (groupid as key) -> (array of user docs as val). This mapping operation seems like a good beginning.
function(doc)
{
  var type = doc.type;
  var groups = doc.groups;
  if(type == "user" && groups.length > 0)
  {
    for(var i = 0; i < groups.length; i++)
    {
      emit(groups[i], doc);
    }
  }
}
But there's obviously something very wrong with my attempt at a reduce:
function(key, values, rereduce)
{
  var set = [];
  var seen = [];
  for(var i = 0; i < values.length; i++)
  {
    var _id = values[i]._id;
    if(seen.indexOf(_id) == -1)
    {
      seen.push(_id);
      set.push(values[i]);
    }
  }
  return set;
}
I'm running CouchDB 0.10dev. Any help appreciated.
© Stack Overflow or respective owner