You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Peter Frisch <pe...@vervis.de> on 2010/04/19 10:25:02 UTC

Map function to find docs in time interval

I have documents that contain two timestamps (visitor entrance time and visitor time of exit). Now I'm trying to write a map function that would allow me to find out which visitors have been "in" at a specific given time. Any suggestions how I would do that?

Right now the timestamps are given as arrays containing year, month, day, hour, minute, second, UTC offset but that could be changed if that doesn't seem to be a wise decision.


RE: Map function to find docs in time interval

Posted by Nils Breunese <N....@vpro.nl>.
Both [year, month, day, hour, minute, second, offset] arrays and RFC 3339 timestamps [0] should both sort nicely as well. I prefer human readable timestamps myself.

Nils.

[0] http://www.ietf.org/rfc/rfc3339.txt
________________________________________
Van: Alan Boyce [alan@alanboyce.com]
Verzonden: maandag 19 april 2010 14:41
Aan: user@couchdb.apache.org
Onderwerp: Re: Map function to find docs in time interval

Hi,

Use epochs. Since they are seconds since 1970 they will sorty nicely.

var startTime = startDate.getTime()/1000.0;
var endTime = endDate.getTime()/1000.0;
emit([startTime, endTime]);


best,

alan

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: Map function to find docs in time interval

Posted by Alan Boyce <al...@alanboyce.com>.
Hi,

Use epochs. Since they are seconds since 1970 they will sorty nicely.

var startTime = startDate.getTime()/1000.0;
var endTime = endDate.getTime()/1000.0;
emit([startTime, endTime]);


best,

alan

On Mon, 19 Apr 2010, Manokaran K wrote:

> On Mon, Apr 19, 2010 at 4:58 PM, Manokaran K <ma...@gmail.com> wrote:
>
>> Following will create a lot of rows but the key size would be small:
>>
>> from_time = x milliseconds
>> till_time = y milliseconds
>>
>> for(var i=from_time; i<=till_time; i += 60000){ //for each visitor, a row
>> for every minute he/she was inside.
>>  emit(i, doc._id);
>> }
>>
>> Then you can query with the time (in millisecs) as the key to get all
>> visitors.
>>
>
>
> Sorry. Instead of key, you'll have to use startkey and endkey
>
> regds,
> mano
>

Re: Map function to find docs in time interval

Posted by Manokaran K <ma...@gmail.com>.
On Mon, Apr 19, 2010 at 4:58 PM, Manokaran K <ma...@gmail.com> wrote:

> Following will create a lot of rows but the key size would be small:
>
> from_time = x milliseconds
> till_time = y milliseconds
>
> for(var i=from_time; i<=till_time; i += 60000){ //for each visitor, a row
> for every minute he/she was inside.
>  emit(i, doc._id);
> }
>
> Then you can query with the time (in millisecs) as the key to get all
> visitors.
>


Sorry. Instead of key, you'll have to use startkey and endkey

regds,
mano

Re: Map function to find docs in time interval

Posted by Manokaran K <ma...@gmail.com>.
Following will create a lot of rows but the key size would be small:

from_time = x milliseconds
till_time = y milliseconds

for(var i=from_time; i<=till_time; i += 60000){ //for each visitor, a row
for every minute he/she was inside.
 emit(i, doc._id);
}

Then you can query with the time (in millisecs) as the key to get all
visitors.

regds,
mano

RE: Map function to find docs in time interval

Posted by Peter Frisch <pe...@vervis.de>.
Unfortunately that won't work. Consider the following documents (leaving out the seconds and UTC offset:

[[2010,04,01,  8,04],
 [2010,04,01,  9,23]]

[[2010,04,01, 10,24],
 [2010,04,02,  6,45]]

[[2010,04,01, 11,03],
 [2010,04,01, 16,20]]

[[2010,04,01, 12,24],
 [2010,04,01, 15,45]]

[[2010,04,02, 10,24],
 [2010,04,02, 13,45]]

Now I want to get back all documents for [2010,04,01, 12,00]

Emitting as you suggested and setting the startkey to [2010,04,01, 11,59, 2010,04,01 12,01] I would not get the third document, but fourth and fifth document. 


Re: Map function to find docs in time interval

Posted by Sebastian Cohnen <se...@googlemail.com>.
my first thought would have been emitting [2010, 01, 01, 5, 0, 0, 2010, 01, 01, 6 , 0, 0] and query with startkey=[2010, 01, 01, 4, 59, 59, 2010, 01, 01, 6, 0, 1] to get documents between 2010-01-01 05:00:00 and 2010-01-01 06:00:00

On 19.04.2010, at 10:25, Peter Frisch wrote:

> I have documents that contain two timestamps (visitor entrance time and visitor time of exit). Now I'm trying to write a map function that would allow me to find out which visitors have been "in" at a specific given time. Any suggestions how I would do that?
> 
> Right now the timestamps are given as arrays containing year, month, day, hour, minute, second, UTC offset but that could be changed if that doesn't seem to be a wise decision.
>