我有mongodb集合中的文档,如下所示:
"listingReservation" : {
"reservationFromDate" : new Date("14.1.2015 00:00:00"),
"reservationToDate" : new Date("17.1.2015 00:00:00"),
"reservationCreatedBy" : "test@test.com",
"reservationCreatedOn" : "",
"reservationChangedBy" : "",
"reservationChangedOn" : "",
"reservationStatus" : "pending" //status can be pedning, accepted or rejected
}
现在,当管理员接受预订时,相同日期范围之间的其他预订和包含已接受预订的预订应设置为拒绝状态。
现在我如何找到新预订日期之间是否已有预订对象,或者是否已经为新预订日期提供了包含预订。
我尝试了以下查询,但显然所有条件都不起作用。
db1.reservationTable.update(
{
$and: [{
$or: [{
$or: [
{
'listingReservation.reservationFromDate': {
$gte: new Date(req.body.listingReservation.reservationFromDate),
$lt: new Date(req.body.listingReservation.reservationToDate)
}
},
{
'listingReservation.reservationToDate': {
$gte: new Date(req.body.listingReservation.reservationFromDate),
$lt: new Date(req.body.listingReservation.reservationToDate)
}
}
],
$and: [
{'listingReservation.reservationFromDate': {$lte: new Date(req.body.listingReservation.reservationFromDate)}},
{'listingReservation.reservationToDate': {$gte: new Date(req.body.listingReservation.reservationToDate)}}
]
}]
},
{'listingBasics.listingId': req.body.listingBasics.listingId},
{_id: {$ne: mongojs.ObjectId(req.body._id)}},
{'listingBasics.listingOwner': req.user.username}
]
},
{
$set: {'listingReservation.reservationStatus': 'rejected'}
})
答案 0 :(得分:2)
要问的逻辑问题是
是否有任何保留:
开始日期早于
req.body.listingReservation.reservationFromDate
,结束日期早于req.body.listingReservation.reservationFromDate
或强>
开始日期早于
req.body.listingReservation.reservationToDate
,结束日期早于req.body.listingReservation.reservationToDate
或强>
开始日期是
req.body.listingReservation.reservationFromDate
之后结束日期是req.body.listingReservation.reservationToDate
之前?
以下是基于您的查询:
db1.reservationTable.update(
{
$or: [
{
'listingReservation.reservationFromDate': {
$lte: new Date(req.body.listingReservation.reservationFromDate)
}, 'listingReservation.reservationToDate': {
$gte: new Date(req.body.listingReservation.reservationFromDate)
}
}, {
'listingReservation.reservationFromDate': {
$lte: new Date(req.body.listingReservation.reservationToDate)
}, 'listingReservation.reservationToDate': {
$gte: new Date(req.body.listingReservation.reservationToDate)
}
}, {
'listingReservation.reservationFromDate': {
$gte: new Date(req.body.listingReservation.reservationFromDate)
}, 'listingReservation.reservationToDate': {
$lte: new Date(req.body.listingReservation.reservationToDate)
}
}
],
'listingBasics.listingId': req.body.listingBasics.listingId,
_id: {$ne: mongojs.ObjectId(req.body._id)},
'listingBasics.listingOwner': req.user.username
}, {
$set: {'listingReservation.reservationStatus': 'rejected'}
})
答案 1 :(得分:2)
如果我正确理解了这个问题,那么您正在查找查询是否有任何预留与日期范围重叠,并将这些预订的状态设置为rejected
。在您的设置方面,执行此操作的标准查询是
db1.reservationTable.update({
"reservationFromDate" : { "$lte" : new Date(req.body.listingReservation.reservationToDate) },
"reservationToDate" : { "$gte" : new Date(req.body.listingReservation.reservationFromDate) }
},
{ "$set" : { "reservationStatus" : "rejected" } },
{ "multi" : true }
)
如果我们将时间段称为重叠T
和S
,则简单英语的条件是T
在S
结束前开始,{{1在T
开始后结束。如果你仔细想想,可以看出这可以保证S
和T
重叠。