You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Adam Groves <ad...@gmail.com> on 2008/10/24 13:48:11 UTC

Another question about modelling joins ..

Hi there,

I'd like to model the following relationships in my database:

 project has_many documents
 document has_many versions

I've written a view which will return all documents plus a version count:

map:
----
function(doc) {
  if(doc.type == "document") {
    emit(doc._id, doc)
  }
  if(doc.type == "version") {
   emit(doc.document_id, 1)
  }
}

reduce:
-------
function(keys,values) {
  var count = 0;
  var doc;
  for(var i = 0 ; i < values.length ; i++) {
    value = values[i];
    if(typeof(value)=="number") {
      count+=i;
    } else {
      doc = value;
    }
  }
  doc.version_counter = count;
  return doc;
}

The request my_database/_view/document/with_version_count?group=true
returns the results I need. However, I'd like to restrict these
results to a specific project. Is that possible? Only documents have a
project_id attribute, versions don't know which project they belong
to. I could always just add a project_id to each version, but I
thought there might be a better way. Any help would be much
appreciated.

Cheers

Adam