0

I have a cloudant database with some ical-Documents with some extra fields like this:

{
  "_id": "ae3630aed08f11151fd022fb1e3fa01a",
  "_rev": "1-ffa2e7c74828943ed78c20eb01135bdb",
  "user": "test@mail.com",
  "startMonth": 4,
  "endMonth": 4,
  "type": "calendarentry",
  "start": "2017-05-22 06:50",
  "end": "2017-05-22 15:00",
  "startTs": 1495428600000,
  "endTs": 1495458000000,
  "title": "Bolder Boulder",
  "description": "Annual 10-kilometer run in Boulder, Colorado",
  "location": "Folsom Field, University of Colorado (finish line)",
  "url": "http://www.bolderboulder.com/",
  "status": "confirmed",
  "geo": {
    "lat": 40.0095,
    "lon": 105.2669
  },
  "attendees": [
    {
      "name": "Adam Gibbons",
      "email": "adam@example.com"
    },
    {
      "name": "Brittany Seaton",
      "email": "brittany@example2.org"
    }
  ],
  "categories": [
    "10k races",
    "Memorial Day Weekend",
    "Boulder CO"
  ],
  "icalEventString": "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nCALSCALE:GREGORIAN\r\nPRODID:-//Adam Gibbons//agibbons.com//ICS: iCalendar Generator\r\nBEGIN:VEVENT\r\nUID:9e37a030-031d-11e7-9481-693e2543922e\r\nDTSTAMP:20170307T100540Z\r\nDTSTART:20170522T065000\r\nDTEND:20170522T150000\r\nSUMMARY:Bolder Boulder\r\nDESCRIPTION:Annual 10-kilometer run in Boulder, Colorado\r\nLOCATION:Folsom Field, University of Colorado (finish line)\r\nURL:http://www.bolderboulder.com/\r\nSTATUS:confirmed\r\nGEO:40.0095;105.2669\r\nATTENDEE;CN=Adam Gibbons:mailto:adam@example.com\r\nATTENDEE;CN=Brittany Seaton:mailto:brittany@example2.org\r\nCATEGORIES:10k races,Memorial Day Weekend,Boulder CO\r\nEND:VEVENT\r\nEND:VCALENDAR"
}

Now i want to query all events that are within a specific period (fully and partially). For example all events in range of t1=timestamp1 and t2=timestamp2. Because the Events have a start and an end i have to look if start or end are in range.

It would be something like:

SELECT * FROM table WHERE NOT((t1 < startTs) AND (t2 < startTs)) OR NOT((t1 > endTs) AND (t2 > endTs))

So every doc where t1 and t2 are less than startTs or greater than endTs are not in the interval.

But i really cant put that in a cloudant selector. Is this even possible? Or maybe there is a better approach to this.

UPDATE:

Ok i finally could write a selector for this problem:

var selector = {
    "selector": {
        "$or": [
            {
                "startTimestamp": {
                    "$gte": start,
                    "$lte": end
                }
            },
            {
                "endTimestamp": {
                    "$gte": start,
                    "$lte": end
                }
            },
            {
                "$and": [
                    {
                        "startTimestamp": {
                            "$lte": start
                        }
                    },
                    {
                        "endTimestamp": {
                            "$gte": end
                        }
                    }]
            }]
    },
    "sort": [
        {
            "startTimestamp": "asc"
        }
    ]
};

There are some problems that it cant use an index although there is one for startTimestamp and endTimestamp. I think i read something about that $or cant use an index. I dont know.

Maybe someone has a better solution for my problem :)

Community
  • 1
  • 1
Roger292
  • 11
  • 3
  • I added a solution with a selector... but i hope there is something with a better performance :) – Roger292 Mar 07 '17 at 15:39
  • Take a look at http://stackoverflow.com/questions/42633029/finding-overlap-date-ranges-between-startdate-and-enddate-in-couch-db-or-cloudan/42637587#4263758. You should be able to use the same approach. Just remove the roomid condition that doesn't apply to your scenario. – ptitzler Mar 07 '17 at 15:49

0 Answers0