8

I'm using CouchDB for storing data about events. Each event has a start date/time and end date/time. I'd like to create a view now, that allows me to get a list of all events that happen on a specific date/time. But the events don't have a single dates, they can rather range over several days.

Now I don't know how I can reflect this in my view function. Unfortunately, I need granularity on minute level, so emitting a key for each minute might not be a valid solution. So how can I do that?

Thanks in advance!

b_erb
  • 20,932
  • 8
  • 55
  • 64

2 Answers2

8

Ok, here's a solution anyway! I just ping-ponged with Jan (Lehnardt) of CouchDB and he told me that I can emit() multiple times within a map. Something I did not know up until now.

To make it easier for myself, I'm assuming your end and start time are TIMESTAMP values already. If not, you need to convert them in your map or generally switch to them.

I'm also gonna assume that an event starts at a full minute (e.g. 16:51:00) and not at 16:51:23. Same on the end date.

Example document:

{
    "_id"   : "super-random-id",
    "name"  : "event 1",
    "start" : "TIMESTAMP",
    "end"   : "TIMESTAMP"
}

Here's the map:

function (doc) {
    if (doc.start == undefined || doc.end == undefined) {
        return;
    }
    var current = doc.start;
    while (current <= doc.end) {
        emit(current, doc.id);
        current = current + 60; // increment by 1 minute
    }
}

Then it should be easy to query with startkey and endkey. You could probably add an _list here.

Till
  • 22,236
  • 4
  • 59
  • 89
  • 1
    Thanks for your answer, but as I already wrote in my question, "emitting a key for each minute might not be a valid solution". This is due to the fact that I have very long time ranges on the hand (i.e multiple days). On the other hand I still need a fine granularity. This can only be achieved by emitting thousands of keys per document, which will not really scale well as far as I know. But thanks again for your thoughts. – b_erb Jun 30 '10 at 17:21
  • Well, views are computed once and then they're read. I don't think it should be an issue, but I see what you mean. – Till Jul 01 '10 at 17:29
4

I found finally a good solution using GeoCouch: http://www.diretto.org/2010/08/efficient-time-based-range-queries-in-couchdb-using-geocouch/

b_erb
  • 20,932
  • 8
  • 55
  • 64
  • 1
    This is the right answer, but unfortunately the link is dead. – dgreisen Mar 08 '15 at 13:41
  • From article: GeoCouch "allows for spatial searches using bounding boxes... GeoCouch’s spatial index expects GeoJSON entries (which are also not bound to any distinct format like WGS84), so we emit our time periods as positions with bounding boxes. The longitudinal values are ignored and set to 0 and the latitudinal values represent the temporal start and end point encoded as time stamps. Thus, we... efficently query all entries within a given period." http://web.archive.org/web/20131121023735/http://www.diretto.org/2010/08/efficient-time-based-range-queries-in-couchdb-using-geocouch/ – dgreisen Mar 08 '15 at 13:45