You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Joe Hillenbrand <jo...@gmail.com> on 2011/04/22 23:21:43 UTC

View based on date/time?

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.

Also, I don't seem to have any way to debug view functions, for finding
errors such as forgetting a semi-colon or finding out that there is no
Date().

I'm running CouchDB 1.0.1

Thanks,

-Joe

Re: View based on date/time?

Posted by Sam Bisbee <sa...@sbisbee.com>.
On Fri, Apr 22, 2011 at 06:03:03PM -0400, Dennis Clark wrote:
> On Fri, Apr 22, 2011 at 5:21 PM, Joe Hillenbrand <jo...@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.

Exactly right - every time you send input to a map function you should get the
same result (assuming the map function's code doesn't change).

Joe, here's an example of what Dennis is talking about for your situation:

function(doc) {
  emit(doc.age, doc.parent);
}

If you still want to query with the document's _id, thereby selecting a
document if it meets a certain time requirement, you would use complex keys
like so:

function(doc) {
  emit([doc._id, doc.age], doc.parent);
}

Then you could query your view like so: ?startkey=[id]&endkey=[id, maxAge]

More information on complex keys:
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Complex_Keys

Cheers,

-- 
Sam Bisbee
www.sbisbee.com


Re: View based on date/time?

Posted by Dennis Clark <db...@gmail.com>.
On Fri, Apr 22, 2011 at 5:21 PM, Joe Hillenbrand <jo...@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.

Re: View based on date/time?

Posted by Jim Klo <ji...@sri.com>.
> Also, I don't seem to have any way to debug view functions, for finding
> errors such as forgetting a semi-colon or finding out that there is no
> Date().


I recommend installing SpiderMonkey locally and using it to debug.  You'll need to have a couple of predefined functions like log and emit in your back pocket, but it certainly has helped me work out the views...

Also have you tried writing the view in Futon?  It spits out some errors and can be handy if you don't want to hassle with installing SpiderMonkey.

- Jim


Jim Klo
Senior Software Engineer
Center for Software Engineering
SRI International