Fetch data in MongoDB based on days

Viewed 44

I am building a parking management app in node-js and utilizing MongoDB as a database.

So first let's understand what my concern is:

  1. The first thing is that, Merchant or the person who has open-plot or any available space for parking will rent (add to app's database) his space to other people at whatever rate that he has decided to. The merchant will not add the parking availability every day.

He will add parking availability data at once(If S/he wants to add data on daily basis then no problem whatsoever but this is my perspective) like this From Mon-Fri the timing for parking availability is 9:00 to 18:00, on saturday it will be 9:00 to 14:00 and on sunday it is unavailable. The thing to note here is that he will not add by DATE

  1. Now, The user who wants to book that available parking space will search it by city/state/area and most importantly DATE. User will not search by Day.

So, my question is, Is there any method to convert that day into date and user can easily find it? Is using day.js library makes it easier?

Availability schema from parkings table:

availability: [
      {
        day: {
          type: String,
        },
        startTime: {
          type: Date,
        },
        endTime: {
          type: String,
        },
      },
    ],
1 Answers

The example that you gave implies that the availability only depends on the day of the week. But is this already the full truth? What about public holidays, or perhaps the parking lot owner's vacations?

If it only depends on the day of the week, you can look up the availability as

availability[DATE.getDay()]

where

const availability = [
  "Availability on Sundays",
  "Availability on Mondays",
  ...
  "Availability on Saturdays"
];

This fulfills your requirement

suppose, user will search for availability on a date 20 july, 2022 and that day is wednesday so merchant should automatically notify that hmm that day is wednesday.

For public holidays and vacations, you would need an extra table lookup.

On the other hand, if you want to offer the parking lot owners more flexibility, you would have to

  • come up with a machine-readable representation (I'd would suggest a text/calendar format)
  • offer a maintenance UI where parking lot owners can specify their availability (they could maintain it in a calendar app of their choice and upload it to your app as text/calendar)
  • offer a display UI where visitors to your app can view the availability calendar and pick a time slot
  • implement a check on your server whether the picked time slot is within the specified availability.

For the last two bullet points (display a calendar and find a time slot in a calendar) there are certainly Node.js libraries to help with that, based on the text/calendar format.

However, text/calendar is a text-based representation and it is not immediately obvious how best to store it on a database.

Related