db查询两个日期列之间的当前日期时间

wz1wpwve  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(417)

如果这是一个非常基本的/noob问题,我很抱歉,但是我对来自sql的db还很陌生。
(1)
下面是我想转换成DB的sql查询。这可能真的很简单,但我不能使它正确。
sql等效项:

  1. select *
  2. from reservations
  3. where
  4. room_id = 'b1a7ddd3-ddfd-4624-8e85-79b47fb19f99' and
  5. now() between reservation_start and reservation_end

数据库查询(有错误):

  1. r.db("myDb").table("reservations").filter(function(doc){
  2. return
  3. doc("room_id").eq("b1a7ddd3-ddfd-4624-8e85-79b47fb19f99")
  4. .and( r.now().between(doc("reservation_start ").date(), doc("reservation_end").date()) )
  5. }
  6. )

我只想返回预定今天,或正在进行,如果它已经开始,但尚未完成(结束日期时间)。
(2)
预订为与会者提供了一个列或字段,即姓名/电子邮件的列表/数组:

  1. attendees: [
  2. {"name": "Attendee 1", "email": "attendee1@test.com"},
  3. {"name": "Attendee 2", "email": "attendee2@test.com"},
  4. {"name": "Attendee 3", "email": "attendee3@test.com"},
  5. ]

我想添加一个过滤器来检查 email 存在于 list of attendees .
这就像查询:电子邮件 attendee2@test.com 今天预订了 room 101 .
如果使用attende电子邮件进行查询是不可能或复杂的。。。我不介意,因为我可以在我的申请表上做检查。重要的是数据库的查询等价于 now() between dateColumnStart and dateColumnEnd .
更新:添加了存储在db(db)中的示例数据

  1. {
  2. "attendees": [
  3. {
  4. "email": dummyUser101@gmail.com, »
  5. "name": "Dummy User 101"
  6. } ,
  7. {
  8. "email": dummyUser102@gmail.com, »
  9. "name": "Dummy User 102"
  10. }
  11. ] ,
  12. "id": "45qum29cel0cm4ejl2obi6pttj" ,
  13. "room_id": "7cc8e51d-e3fa-4d84-b7e6-9ebf8975754a" ,
  14. "reservation_end": "2018-11-23T02:00:00" , //10AM (GMT8)
  15. "reservation_start": "2018-11-19T00:00:00" , //8AM (GMT8)
  16. "details": "Week event 8AM-10AM Test"
  17. }
  18. {
  19. "attendees": [
  20. {
  21. "email": dummyUser103@gmail.com, »
  22. "name": "Dummy User 103"
  23. } ,
  24. {
  25. "email": dummyUser101@gmail.com, »
  26. "name": "Dummy User 101"
  27. } ,
  28. {
  29. "email": dummyUser102@gmail.com, »
  30. "name": "Dummy User 102"
  31. }
  32. ] ,
  33. "id": "6ejq8h6tvlpnjiskvt4kthfmss_20181123T060000Z" ,
  34. "room_id": "7cc8e51d-e3fa-4d84-b7e6-9ebf8975754a" ,
  35. "reservation_end": "2018-11-23T07:00:00" , //3PM (GMT8)
  36. "reservation_start": "2018-11-23T06:00:00" , //2PM (GMT8)
  37. "details": "Test Reservation"
  38. }

谢谢!

sg2wtvxw

sg2wtvxw1#

(1)
不能在筛选器中使用between。在两个键之间获取所有文档。请参阅以下文档:https://www.rethinkdb.com/api/javascript/between/
你需要的是“在”关键字。

  1. r.db("myDB").table("reservations").filter(function(doc){
  2. return doc("room_id").eq("b1a7ddd3-ddfd-4624-8e85-79b47fb19f99")
  3. .and( r.now().during(
  4. r.ISO8601(doc("reservation_start"),{defaultTimezone:"+08:00"}),
  5. r.ISO8601(doc("reservation_end"),{defaultTimezone:"+08:00"})))
  6. }
  7. )

相关问题