incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Adam Groves" <adam.gro...@gmail.com>
Subject Another question about modelling joins ..
Date Fri, 24 Oct 2008 11:48:11 GMT
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

Mime
View raw message