couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dennis Clark <dbfcl...@gmail.com>
Subject Re: View based on date/time?
Date Fri, 22 Apr 2011 22:03:03 GMT
On Fri, Apr 22, 2011 at 5:21 PM, Joe Hillenbrand <joehillen@gmail.com>wrote:

> I'm trying to write a view that only shows documents that are less than an
> hour old.
>
> My map function looks like this:
>
> function (doc){
>        var now = new Date().getTime();
>        var age = now - Date.parse(doc.date);
>        if (age < 1*1000*60*60){
>            emit(doc._id, doc.parent);
>        }
> }
>
> This code works in the browser. But when I run it as a view I get no
> results.
>
> I'm guessing that CouchDB's javascript library doesn't have a Date()
> object.
> Now would I find that out? I can't seem to find any documentation for what
> CouchDB's javascript does and does not have.
>

Maps and reduces in views are required to be referentially transparent --
that is, the output can depend only on the input, rather than the input and
some other state that you go get (here the time). The problem is that the
map function is not run on each document for each query -- instead, the
results are cached and only recomputed if the underlying documents have
changed. The right idiom for "documents less than an hour old" is to emit
the document timestamp, then query with startkey=an hour ago. There are
reasons to use other kinds of things for this task, but they depend on your
use case.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message