couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wolfgang haefelinger <>
Subject Re: getting most recent doc
Date Fri, 16 Apr 2010 08:46:40 GMT
Thanks Robert

for your answer. However, it is not exactly what I was looking for
(due to my inappropriate problem description).

Firstly, I do want to have the document instead of the time stamp in
order to avoid that additional document fetch. That's obviously easy
to fix:

function(doc) { //
 emit([, doc.timestamp], doc);

However, in my original attempt I can easily get an overview over all
categories using "group=true". And again my apologies for not having
mentioned this - that's what I actually want:
* I want to have an overview over all "categories" where each category
is associated with the most recent event (document).
* Another goal is to limit the traffic to a minimum, i.e. I don't want
to discard or skip items returned by the (couchdb) server.

Your map function works fine - if I know the category names in
advance. Assume that I have N categories. Then I have to query the
server N+1 times in order to present the overview (1 query to get all
categories and N queries to get most recent event document).

This is exactly the same as my second attempt: 1 query to get
categories and most recent document id plus N simple get queries
fetching documents. But what I was wondering is, whether I can avoid
that extra N queries?

// Regards

Obviously is your example more flexible as it would very easily allow
me to calculate the most N recent events.

Btw, I had some hard times regarding parameter "descending" which is
IMHO harmful documented ("reverse the output" => no, not at all!) and
inappropriate named. What "descending" does is changing the "key
traversal order", not more and not less. The output of that traversal
is unchanged.

On Fri, Apr 16, 2010 at 12:42 AM, Robert Newson <> wrote:
> Something like;
> map;
> function(doc) {
>  emit([, doc.timestamp], null);
> }
> no reduce method.
> with calls like;
> http://localhost:5984/db/_design/ddoc/_view/view?startkey=["name",{}]&endkey=["name"]&descending=true&limit=1
> should get you the latest (highest timestamp) for document with
> of "name"
> On Thu, Apr 15, 2010 at 11:33 PM, wolfgang haefelinger
> <> wrote:
>> My apologies for asking a probably rather stupid question ..
>> Docs in my database are events. An event has a category name, a time
>> stamp and lots of other details. I am looking for the most recent
>> event.
>> I started with something like
>> function (doc) {              // map
>>  emit(,doc)
>> }
>> function (ks,vs,rr) {
>>  var maxdoc = max_timestamp(vs);
>>  return maxdoc;
>> }
>> Although I'm reducing considerably, couchdb complains that I'm not
>> reducing hard enough :-)
>> (* Btw, what is actually the "reduce" limit? Do attachments count as well?  *)
>> Then I tried something like
>> function (doc) {
>>  emit(,{ "_id" : doc._id, "ts" : doc.timestamp});
>> }
>> and with a "virtually" unchanged reduce function. Now I'm below
>> couchdb's reduction limit. However, this view does not return the most
>> recent document I was looking for but the "id" of that document.
>> I had some hope that query parameter "include_docs=true" would do the
>> magic (that's why I used "_id" as property name). But then I learned
>> that "include_docs" is not supported for "reduced" views which took me
>> by surprise. What could be the rational for this behaviour?
>> In summary:
>> * Is there a way to get the "most recent" document via a view?
>> // Regards

Wolfgang Häfelinger
häfelinger IT - Applied Software Architecture
+31 648 27 61 59

View raw message