【问题标题】:Booking System MongoDB, Apollo prevent bookings on same room预订系统 MongoDB、Apollo 阻止在同一个房间进行预订
【发布时间】:2018-12-05 15:31:08
【问题描述】:

我尝试用 MongoDB 作为数据库、Apollo GraphQL 和 express 服务器来实现一个房间预订系统。

我必须在我的数据库中收集:一份用于房间,一份用于预订。为了检查可用性,系统会查找数据库中当前的所有预订,并检查在指定时间段内哪些房间是可用的。 如果系统预订了房间,它会在数据库中插入一条新记录。

问题是以下场景,同一房间有两个预订请求:

booking request 1 -> check for available rooms
booking request 2 -> check for available rooms
booking request 1 -> read request success (display available rooms)
booking request 2 -> read request success (display available rooms)
booking request 1 -> write a new record to the db (new booking)
booking request 2 -> write a new record to the db (new booking) but this conflicts with booking 1.

我如何保证一个房间永远不会发生冲突预订?

问题是,我有一个可用性请求和另一个将预订写入数据库的语句。

【问题讨论】:

  • 我想我真正的意思是,有没有办法防止幻读以防止重复预订?

标签: mongodb concurrency


【解决方案1】:

我对 postgres 做了同样的事情,但我确信 MongoDB 具有相同的功能。

在您的预订表中,键将是房间和日期范围的组合。

你可以在桌子上定义约束,相同 room_id 的 raw 不能有重叠 日期范围。

当您要插入新的预订时,只需插入它,数据库应避免插入与时间范围重叠的原始数据(由于约束)

【讨论】:

  • 非常感谢您的回答。据我所知,这种约束对于 MongoDB 是不可能的。我可能使用了错误的技术堆栈,应该切换到 sql 数据库。但在我看来,这种简单的预订系统应该可以用 MongoDB 实现,我错了吗?!
猜你喜欢
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多