MongoDB:Mapreduce必要吗?在预订应用程序中对日期进行范围查询 [英] MongoDB: Mapreduce necessary? Range query on dates in a booking application
问题描述
我的收藏集具有以下简化(预订)模式:
My collection has the following simplified (booking) schema:
{
name: "room 1",
from: ISODate("2014-06-10T12:00:00Z"),
to: ISODate("2014-06-14T12:00:00Z")
},
{
name: "room 1",
from: ISODate("2014-06-25T12:00:00Z"),
to: ISODate("2014-06-27T12:00:00Z")
},
{
name: "room 2",
from: ISODate("2014-06-12T12:00:00Z"),
to: ISODate("2014-06-26T12:00:00Z")
}
我想查询一下,如果房间在给定范围内可用.例如,我想知道是否
I'd like to query, if a room is available in a given range. For example I'd like to know if
- 房间1在2014年6月11日至2014年6月13日可用
- 房间1在2014年6月13日至2014年6月26日可用
- 1室从2014-06-15到2014-06-18可用
推荐答案
好吧,让我们分解一下,有4种不同的预订冲突:
Ok let's break this down, there are 4 ranges of booking conflicts:
- 另一笔预订的开始时间早于此开始时间,但结束时间也早于此预订时间结束
- 另一个预订的开始是在这个开始之后,而结束是在这个结束之后
- 另一笔预订的开始时间是在此开始之后,结束时间是在此结束之前
- 其他预订的开始时间在此开始之前和结束之后
因此,您正在寻找一个查询,该查询可以找到所有这些范围(如果存在).
So you are looking for a query that can find all those ranges if they exist.
现在,我设置以下数据:
Now I set up this data:
> db.rooms.find()
{ "_id" : ObjectId("53ad206e1d8f2d8351182830"), "id" : 1, "from" : ISODate("2014-06-26T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad276f1d8f2d8351182831"), "id" : 1, "from" : ISODate("2014-06-24T00:00:00Z"), "to" : ISODate("2014-07-01T00:00:00Z") }
{ "_id" : ObjectId("53ad28ad1d8f2d8351182832"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad28c61d8f2d8351182833"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-07-03T00:00:00Z") }
{ "_id" : ObjectId("53ad29971d8f2d8351182834"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-21T00:00:00Z") }
(第5个范围是测试人员,以确保查询不会返回随机结果)
(the 5th range being a tester to make sure the query doesn't return random results)
然后我跑了:
> db.rooms.find({from:{$lte: ISODate('2014-06-30T00:00:00.000Z')}, to:{$gte: ISODate('2014-06-23T00:00:00.000Z')}})
{ "_id" : ObjectId("53ad206e1d8f2d8351182830"), "id" : 1, "from" : ISODate("2014-06-26T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad276f1d8f2d8351182831"), "id" : 1, "from" : ISODate("2014-06-24T00:00:00Z"), "to" : ISODate("2014-07-01T00:00:00Z") }
{ "_id" : ObjectId("53ad28ad1d8f2d8351182832"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-06-28T00:00:00Z") }
{ "_id" : ObjectId("53ad28c61d8f2d8351182833"), "id" : 1, "from" : ISODate("2014-06-20T00:00:00Z"), "to" : ISODate("2014-07-03T00:00:00Z") }
如果该查询返回,则表明有预订,因此您不能插入.
If that query returns there are bookings and as such you cannot insert.
那应该涵盖我认为的所有基础,我有点睡眠不足,所以我可能是错的.
That should cover all bases I think, I am a little sleep deprived so I might be wrong.
这篇关于MongoDB:Mapreduce必要吗?在预订应用程序中对日期进行范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!