如何查看 laravel 和 mongo 中的可用日期
How to check available dates in laravel and mongo
我正在使用 laravel 和 mongodb 开发酒店预订可用性检查。我很困惑编写查询以查找可用日期。我尝试了一种方法,但它就像 between and
。我不确定查询是否正确。
我在这个问题中包含了 laravel 原始查询和 mongo 查询。两者相同。
为什么查询不正确?
因为,访客在 2017-09-02
签到并在 2017-09-05
签出。目前我正在获取 2017-09-02 >= checkin_from && 2017-09-05 <= checkin_from
之类的数据。如果 2017-09-02
是 checkin_from
日期那么,这是正确的。但是如果 checkin_from
是 2017-08-25
并且 reserve_to
是 2017-09-06
。这里日期 2017-09-02 to 2017-09-05
包括在内。在这种情况下,我们将如何检查?
这可以通过查询实现吗?
或
数组 1:获取所有预订并存储在数组中。
数组 2:然后使用 DatePeriod 和 DateInterval 准备日期(来自前端的签入和签出日期)并存储在数组中。然后检查匹配数组(1 & 2)。
我应该遵循哪种方法?
查询我目前正在使用
$bookings = Booking::raw(function ($collection) use ($dateBegin, $dateEnd) {
return $collection->aggregate([
[
'$match' => [
'cabinname' => 'test',
'checkin_from' => ['$gte' => $dateBegin, '$lte' => $dateEnd],
'is_delete' => 0,
],
],
[
'$project' => [
'beds' => 1,
'dormitory' => 1,
'sleeps' => 1,
'status' => 1,
'has status' => [
'$in' => ['status', ['1', '4', '5', '7']]
]
],
]
]);
});
Mongo查询
db.booking.aggregate([
{
$match: {
"cabinname" : 'Test',
"checkin_from" : {$gte :[ new Date ('2017-09-01') ], $lte : [ new Date ('2017-09-03') ] },
"is_delete" : 0,
}
},
{
$project: {
"beds" : 1,
"cabinname":1,
"checkin_from":1,
"reserve_to":1,
"has status" : {
$in: [ "status", ['1', '4', '5', '7'] ]
}
}
}
])
来自数据库的数据
{ "_id" : ObjectId("5888fbd5d2ae672245fb5f79"), "cabinname" : "Test", "checkin_from" : ISODate("2017-08-29T22:00:00Z"), "reserve_to" : ISODate("2017-09-03T22:00:00Z"), "beds" : "8" }
{ "_id" : ObjectId("58a4812bd2ae67066eeaea41"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-01T22:00:00Z"), "reserve_to" : ISODate("2017-09-05T22:00:00Z"), "beds" : "18" }
{ "_id" : ObjectId("58bac8a5d2ae67951845edaf"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-01T22:00:00Z"), "reserve_to" : ISODate("2017-09-02T22:00:00Z"), "beds" : "0" }
{ "_id" : ObjectId("58d03541d2ae671c668b4568"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-02T22:00:00Z"), "reserve_to" : ISODate("2017-09-04T22:00:00Z"), "beds" : "14" }
由于条件错误,我的查询无法正常工作。我已经更新了条件,现在我得到了准确的结果。
{checkin_from:{$lte: new Date ('2017-09-02')}, reserve_to:{$gt: new Date ('2017-09-02')}}
$bookings = Booking::raw(function ($collection) use ($dateBegin, $dateEnd) {
return $collection->aggregate([
[
'$match' => [
'cabinname' => 'test',
'checkin_from' => ['$lte' => $dateBegin],
'reserve_to' => ['$gt' => $dateBegin],
'is_delete' => 0,
],
],
[
'$project' => [
'beds' => 1,
'dormitory' => 1,
'sleeps' => 1,
'status' => 1,
'has status' => [
'$in' => ['status', ['1', '4', '5', '7']]
]
],
]
]);
});
我正在使用 laravel 和 mongodb 开发酒店预订可用性检查。我很困惑编写查询以查找可用日期。我尝试了一种方法,但它就像 between and
。我不确定查询是否正确。
我在这个问题中包含了 laravel 原始查询和 mongo 查询。两者相同。
为什么查询不正确?
因为,访客在 2017-09-02
签到并在 2017-09-05
签出。目前我正在获取 2017-09-02 >= checkin_from && 2017-09-05 <= checkin_from
之类的数据。如果 2017-09-02
是 checkin_from
日期那么,这是正确的。但是如果 checkin_from
是 2017-08-25
并且 reserve_to
是 2017-09-06
。这里日期 2017-09-02 to 2017-09-05
包括在内。在这种情况下,我们将如何检查?
这可以通过查询实现吗?
或
数组 1:获取所有预订并存储在数组中。
数组 2:然后使用 DatePeriod 和 DateInterval 准备日期(来自前端的签入和签出日期)并存储在数组中。然后检查匹配数组(1 & 2)。
我应该遵循哪种方法?
查询我目前正在使用
$bookings = Booking::raw(function ($collection) use ($dateBegin, $dateEnd) {
return $collection->aggregate([
[
'$match' => [
'cabinname' => 'test',
'checkin_from' => ['$gte' => $dateBegin, '$lte' => $dateEnd],
'is_delete' => 0,
],
],
[
'$project' => [
'beds' => 1,
'dormitory' => 1,
'sleeps' => 1,
'status' => 1,
'has status' => [
'$in' => ['status', ['1', '4', '5', '7']]
]
],
]
]);
});
Mongo查询
db.booking.aggregate([
{
$match: {
"cabinname" : 'Test',
"checkin_from" : {$gte :[ new Date ('2017-09-01') ], $lte : [ new Date ('2017-09-03') ] },
"is_delete" : 0,
}
},
{
$project: {
"beds" : 1,
"cabinname":1,
"checkin_from":1,
"reserve_to":1,
"has status" : {
$in: [ "status", ['1', '4', '5', '7'] ]
}
}
}
])
来自数据库的数据
{ "_id" : ObjectId("5888fbd5d2ae672245fb5f79"), "cabinname" : "Test", "checkin_from" : ISODate("2017-08-29T22:00:00Z"), "reserve_to" : ISODate("2017-09-03T22:00:00Z"), "beds" : "8" }
{ "_id" : ObjectId("58a4812bd2ae67066eeaea41"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-01T22:00:00Z"), "reserve_to" : ISODate("2017-09-05T22:00:00Z"), "beds" : "18" }
{ "_id" : ObjectId("58bac8a5d2ae67951845edaf"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-01T22:00:00Z"), "reserve_to" : ISODate("2017-09-02T22:00:00Z"), "beds" : "0" }
{ "_id" : ObjectId("58d03541d2ae671c668b4568"), "cabinname" : "Test", "checkin_from" : ISODate("2017-09-02T22:00:00Z"), "reserve_to" : ISODate("2017-09-04T22:00:00Z"), "beds" : "14" }
由于条件错误,我的查询无法正常工作。我已经更新了条件,现在我得到了准确的结果。
{checkin_from:{$lte: new Date ('2017-09-02')}, reserve_to:{$gt: new Date ('2017-09-02')}}
$bookings = Booking::raw(function ($collection) use ($dateBegin, $dateEnd) {
return $collection->aggregate([
[
'$match' => [
'cabinname' => 'test',
'checkin_from' => ['$lte' => $dateBegin],
'reserve_to' => ['$gt' => $dateBegin],
'is_delete' => 0,
],
],
[
'$project' => [
'beds' => 1,
'dormitory' => 1,
'sleeps' => 1,
'status' => 1,
'has status' => [
'$in' => ['status', ['1', '4', '5', '7']]
]
],
]
]);
});