在沙发Db或cloudant中查找开始日期和结束日期之间的重叠日期范围
您好,我正在使用数据库作为couchDb构建预订应用程序。我有几个预订文件,每个文件都有roomId,开始日期和结束日期。
Hello I am building a reservation app using database as couchDb. I have several reservation documents and each of them has roomId, start date and end date.
现在,当用户创建带有roomId,开始日期和结束日期的会议请求时,我需要在现有预订中搜索开始时间和结束时间之间的重叠时间范围,仅在没有冲突时创建预订。与此同时,我还需要检查roomid。
Now when user creates a meeting request with roomId, start date and end date, I need to search for overlaps time ranges between the start time and endtime in the existing reservations and create a reservations only when there is no conflict. Along with this I also need to check for roomid.
该要求类似于确定两个日期范围是否重叠。
我已经在沙发上创建了一个视图,并发出了三个键:
I had created a view on my couch db emitting three keys:
function (doc) {
if (doc.type == "reservation") {
emit([doc.roomid, doc.startTime, doc.endTime], doc);
}
}
我确实尝试创建
?startkey=["1970-01-01T00:00:00Z", ""]&endkey=["\ufff0", "1971-01-01T00:00:00Z"]
但是我并没有真正了解如何复合查询
However I am not really getting how to compound query the view to find range of date along with the roomid.
任何帮助将不胜感激。
您可以使用 Cloudant查询并指定参考答案中概述的(StartA< = EndB)和(EndA> = StartB)
搜索条件。
You could use Cloudant Query and specify the (StartA <= EndB) and (EndA >= StartB)
search condition that's outlined in the referenced answer.
创建索引
向 POST 请求c> _index 端点,将以下JSON数据结构作为有效负载传递。
Send a POST
request to the _index
endpoint, passing the following JSON data structure as payload.
POST https://$USERNAME:$PASSWORD@$HOST/$DATABASE/_index HTTP/1.1
{
"index": {
"fields": [
{ "name":"startTime",
"type":"string"
},
{
"name":"endTime",
"type":"string"
},
{
"name":"roomid",
"type":"string"
}
]
},
"type": "text"
}
查询索引
将 POST
请求发送到 _find
端点,将以下JSON数据结构作为有效负载传递。
Send a POST
request to the _find
endpoint, passing the following JSON data structure as payload.
POST https://$USERNAME:$PASSWORD@$HOST/$DATABASE/_find HTTP/1.1
{
"selector": {
"startTime": {
"$lte": "2017-03-06T15:00:00Z"
},
"endTime": {
"$gte": "2017-03-06T14:00:00Z"
},
"roomid": {
"$eq": "room 123"
}
}
}
根据需要替换时间戳和房间标识符值。如果查询返回至少一个文档,则您遇到预订冲突。
Replace the timestamp and room identifier values as needed. If the query returns at least one document you've encountered a booking conflict.