I have documents in mongodb collection that looks like below:
"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
}
Now when the admin accepts a reservation, other reservation between the same date ranges and reservations which is encompassing the accepted reservation should be set to rejected status.
Now how can I find if there are already reservations objects between the new reservation dates or if there is already an encompassing reservation for the new reservation dates.
I tried below query but obviously all conditions are not working.
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'}
})