couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <>
Subject Re: counting rows in range queries
Date Thu, 30 Jul 2009 07:58:59 GMT
On Thu, Jul 30, 2009 at 02:38:15AM +0200, Fabio Forno wrote:
> Now the difficult part. There are many consumers who retrieve the
> items from the buckets and each consumers knows the date of the last
> item it has retrieved, so I do a range query on the view with
> startkey=[item.bucket, last_date], endkey=[item.bucket, stopper]. This
> is fine, but if I want to tell the user how many items it has to
> retrieve before actually downloading them I can't do nothing but
> retrieving all the rows and start counting.

I'm not sure I have understood the problem correctly, because:

(1) if you do a view fetch with limit=0 in the query string, you'll get
the count without the documents.

(2) you can have a simple counter reduce function, and then do a reduce
across the range, which will give you the number of documents in that range.

    function(ks, vs, co) {
      if (co) {
        return sum(vs);
      } else {
        return vs.length;

Indeed I think there's a built-in _count reduce function now, but I haven't
tried it myself.

> The only possible solution I've found so far is storing marker
> documents for each possible consumer (containing just the item id) any
> time I post a new item in a bucket. These can be counted with a
> map/reduce and deleted when the items are retrieved, but I really
> don't like the solution since it requires too many writes.

That's why I think I don't understand your problem. You said each consumer
already knows the date of the last item it has retrieved, so why would you
store it again?



View raw message