Range Keys in CouchDB Views

后端 未结 2 1520
时光说笑
时光说笑 2020-12-30 11:39

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 eve

相关标签:
2条回答
  • 2020-12-30 12:11

    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.

    0 讨论(0)
  • 2020-12-30 12:34

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

    0 讨论(0)
提交回复
热议问题