I don't think that will work. the "new Date().getTime()" is evaluated
once for each document, so your expectation that documents will fall
out of the view as time moves on will not be met; unchanged documents
are not updated in the view.

All I can think of is two views with a client-side join.

valid_from: {
"map":
function(doc) {
  emit(doc.valid_from, null);
}}

and;

valid_until:{
"map:"
function(doc) {
  emit(doc.valid_until, null);
}}

You'd query the first with valid_from?startkey=<current time> and the
latter with valid_until?endkey=<current time> and then "valid"
documents are documents that are returned by both views. They're
returned in order, so the join logic should be simple enough.

Caveat: Haven't tried this :)

B.

On Fri, Aug 7, 2009 at 10:15 AM, Mario Müller<mario.mueller....@me.com> wrote:
> Hi folks,
>
> I'm new to couchdb (so please don't hurt me ;)). I want to build a view of
> valid documents. Valid, in this case, means that those documents have a
> "valid_from" and a "valid_until" date and "now" must be within this range.
>
> I've written a map script to do the work for me, but I do not know if this
> is a good way for couchdb...? The dates are in mysql datetime format (for
> historical reason)
>
> function(doc) {
>   var convert = {
>       "do": function(timestamp) {
>           var regex = /^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9])
> (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
>           var parts = timestamp.replace(regex, "$1 $2 $3 $4 $5 $6").split('
> ');
>           return new Date(parts[0], parts[1] - 1, parts[2], parts[3],
> parts[4], parts[5]).getTime();
>       },
>   };
>   var iT = new Date().getTime();
>   var iS = convert.do(doc.valid_from);
>   var iEnd = convert.do(doc.valid_unitl);
>   if (iS < iT < iEnd) {
>       emit(doc._id, null);
>   }
> }
>
> I'll be thankful for any kind of comment.
>
> Thanks!
> Mario
> ------------------------------------
> Mario Mueller
> Ellerstraße 130
> 40227 Duesseldorf, Germany
>
> mail. mario.mueller....@me.com
> phon. 0049 176 83016418
> icq.     436092688
> xing.  http://tinyurl.com/MarioMueller
> blog.  http://tinyurl.com/MariosBlog
>
> Attached to this mail you may find a PGP Key file (PGP.sig).
> If you are not using PGP anyway you can ignore it.
>
>

Reply via email to