incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: Tracking view updates with CouchDB
Date Thu, 16 Apr 2009 16:58:42 GMT
On Thu, Apr 16, 2009 at 09:21:59AM -0700, Adam Wolff wrote:
> So we've got docs that look like this:
> {
>   type: "key_doc",
>   _id : "xyzzy",
>   key : "doc_key",
>   updated : 1239896906303
> }
> 
> and docs that look like this:
> {
>   type: "ref_doc",
>   key_doc_id : "xyzzy",
>   updated : 1239897055080
> }
> 
> and we want a view that we query like this:
>   startkey : ["doc_key",null], endkey : ["doc_key",{}]

Not sure if this is exactly what you want, but maybe try something along
these lines:

// MAP
function(doc) {
  var type = doc['type'];
  switch(type) {
  case 'key_doc':
    if (doc.key && doc.updated) {
      emit(doc.key, doc.updated);
    }
    break;
  case 'ref_doc':
    if (doc.key_doc_id && doc.updated) {
      emit(key_doc_id, doc.updated);
    }
  }
}

Then your startkey..endkey view will show all update timestamps referring to
this document.

Here are some other tricks you could try:

* emit(..., -updated)

  Then a limit=1 query should give you the (negated) most recent
  timestamp

* use a reduce function to find the maximum updated timestamp. I came across
  an example recently of how to do that, but can't remember exactly where.
  It also showed how to reduce both the max and min values simultaneously.

> which reduces to the key_doc ids that were updated since the startkey
> date, in order, e.g.
>   ["xyzzy"] in this case

Maybe you want to emit(updated, doc.key) instead, so you can query your view
for all changes made after a particular time. Then it's easy on the client
side to do a uniq on all the doc refs seen.

Regards,

Brian.

Mime
View raw message