如何编写cloudant查询(查询两个区间)

时间:2017-03-07 10:22:12

标签: cloudant nosql

我有一个cloudant数据库,其中包含一些带有一些额外字段的文档 -

{
  "_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"
}

现在我想查询特定时间段内的所有事件(完全和部分)。例如,范围为t1 = timestamp1和t2 = timestamp2的所有事件。因为事件有一个开始和结束,我必须查看开始或结束是否在范围内。

这将是:

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

因此,每个t1和t2小于startTs或大于endTs的文档都不在该区间内。

但我真的不能把它放在云量选择器中。这甚至可能吗?或者也许有更好的方法。

更新:

好的,我终于可以为这个问题编写一个选择器:

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

虽然有一个用于startTimestamp和endTimestamp,但是有些问题无法使用索引。我想我读过一些关于$或者不能使用索引的内容。我不知道。

也许某人对我的问题有更好的解决方案:)

0 个答案:

没有答案