Search code examples
node.jsmongodbexpressmongoosemongoose-schema

Mongo Query for Booking Between


mongodb db Booking schema

Having the bike id, how to check if new booking is between the booked dates ie: bookingStart and bookingEnd. Here is a sample of the document:

Booking data


Solution

  • You can try like below

    If you are using mongoose then in your model file define a function and write code like below

     var customDate = "28/06/2020"
        Booking.find({
               'bike':'5ef31807335f5541209e4546'
               'bookingStart': {$gte: newDate(customDate) },
               'bookingEnd':{$lte: newDate(customDate)}
          },(err,resdata)=>{
               
          })
    

    If you are not using mongoose then you can simply run the following query

    db.bookings.find({'bike':'5ef31807335f5541209e4546'
               'bookingStart': {$gte: newDate('28/06/2020') },
               'bookingEnd':{$lte: newDate('28/06/2020')}})