incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nitin Borwankar <ni...@borwankar.com>
Subject Re: Best practice for date dependend maps
Date Fri, 07 Aug 2009 10:43:23 GMT
Hi Mario,

Looks fine to me ( I didn't look at the regex detials just at the couch
stuff.
Except you may want to add a check like

if(doc.valid_from) {....}
if(doc.valid_until) {...}

This way if a doc doesn't have one of these it will just be skipped.
Otherwise you might get a "can't format undefined as JSON" error message.
This happens when a view is trying to access an attribute that doesn't exist
and the map function won't complete successfully.
This is my feeble understanding so far.

Nitin

37% of all statistics are made up on the spot
-------------------------------------------------------------------------------------
Nitin Borwankar
nborwankar@gmail.com


On Fri, Aug 7, 2009 at 2:15 AM, Mario M├╝ller <mario.mueller.mac@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.mac@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.
>
>

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