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: Problems with reduce in view appear when record size > 6
Date Fri, 31 Jul 2009 07:59:16 GMT
(Please reply to the list, not just to me privately)

On Thu, Jul 30, 2009 at 06:45:00PM -0400, Jochen Kempf wrote:
>    OK, so what is the recommended way to reduce and group records by
>    fields to get both the count and an array of further non-grouped
>    fields?
>    Here is an example:
>    I want to fetch all records that have the same values in the fields
>    "year", "month", "day" and "category".

function(doc) {
  emit([doc.year||null,
        doc.month||null,
        doc.day||null,
        doc.category||null], null);
}

and query with startkey=["Y","M","D","C"]&endkey=["Y","M","D","C",{}]
(suitably URL-encoded)

>    And additionally I would like to be able to access a hash of ids, revs
>    and further record specific fields for each of these fetched records.

You get the ids already.

To get additional data from each record, the simplest way is to add
&include_docs=true to the query.

If your docs are huge and you don't want to retrieve them in their entirety,
then you can emit just the values of interest, either as an array or an
object:

function(doc) {
  emit([doc.year||null,
        doc.month||null,
        doc.day||null,
        doc.category||null],
       {rev: doc._rev, foo: doc.foo||null});
}

That is: when you query the view and there are 100 matching records, you
will get back an array of 100 rows of the form

  rows: [
    {"id":..., "key":[...], "value":{...}},
    ...
  ]

With include_docs=true you'll also get "doc":{...}

Obviously it's trivial to find the size of this array client-side, and then
the client has all the data you asked for ("specific fields for each of
these fetched records")

If you want to prefetch just a count of the records, without retrieving
them, use the same view but with a _count reduce function. You can then
query again with reduce=false to get the view without reduce as above.

Or am I missing something here?

Brian.

Mime
View raw message