如何查询日期范围之间的对象

How to query for objects between date ranges

我在 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'}
            })

要问的逻辑问题是

Are there any reservations where:

the start date is beforereq.body.listingReservation.reservationFromDateand the end date is afterreq.body.listingReservation.reservationFromDate

or

the start date is beforereq.body.listingReservation.reservationToDateand the end date is afterreq.body.listingReservation.reservationToDate

or

the start date is afterreq.body.listingReservation.reservationFromDateand the end date is beforereq.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'}
  })

如果我对问题的理解正确,您正在寻找一个查询来查找是否有任何预订与日期范围重叠,并将这些预订的状态设置为 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 }
)

如果我们将测试重叠的时间段称为 TS,用简单的英语来说,条件是 TS 结束之前开始,并且 TS 开始后结束。如果您考虑一下,您会发现这保证 TS 重叠。