在沙发Db或cloudant中查找开始日期和结束日期之间的重叠日期范围 [英] Finding Overlap date ranges between startdate and enddate in couch Db or cloudant

查看:49
本文介绍了在沙发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.

创建索引

向<$ c $发送 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.

这篇关于在沙发Db或cloudant中查找开始日期和结束日期之间的重叠日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆