简体   繁体   中英

Mongoose find with conflicting dates

I have a mongoose model that holds a start and end date of an event.

I want a query that takes a start and end date and see if it conflicts with any of the events in the database.

Something like:

'select where' - 
    endDate < currentBooking.endDate && startDate < currentBooking.startDate) || end > currentBooking.endDate && startDate > currentBooking.startDate

But how can I write that as a mongoose query?

Kindly test this,hope this will solve your problem

Ebook.find({
  $or: [{
     $and: [{ endDate: { $lte: currentBooking.endDate } }, { startDate: { $lte: 
     currentBooking.startDate}}],
   $and: [{ endDate: { $gte: currentBooking.endDate } }, { startDate: { $gte: 
  currentBooking.startDate}}]
    }]
},function(err, data) {
        if (err)
            res.send(err);

        res.json(data);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM