3

I have a sample collection as following in mongodb database:

{
  "merchantId": {
    "$oid": "62c950dfc96c2b690028be88"
  },
  "contactInfo": {
    "name": "Claudia Shields",
    "phoneNumber": 8904672101
  },
  "location": {
    "address": "737 applegate court",
    "city": "bowie",
    "state": "rhode island",
    "country": "greece",
    "zipCode": 10825
  },
  "price": 16,
  "parkingType": "residence",
  "parkingInfo": [
    {
      "parkingName": "Fountain Avenue",
      "_id": {
        "$oid": "62d12053cb03235286511d55"
      },
      "default": []
    }
  ],
  "totalSpots": [
    127
  ],
  "coordinates": {
    "lng": 34.048954,
    "lat": 10.299556
  },
  "status": "active",
  "isFeePaid": false,
  "parkingZone": [],
  "availability": [
    {
      "day": "Saturday",
      "startTime": "09:00",
      "endTime": "14:00",
      "_id": {
        "$oid": "62d13355bd1ad85d85ae4b4b"
      }
    },
    {
      "day": "Monday",
      "startTime": "08:00",
      "endTime": "16:00",
      "_id": {
        "$oid": "62d13375bd1ad85d85ae4b4f"
      }
    },
    {
      "day": "Tuesday",
      "startTime": "08:00",
      "endTime": "16:00",
      "_id": {
        "$oid": "62d1337cbd1ad85d85ae4b53"
      }
    },
    {
      "day": "Wednesday",
      "startTime": "08:00",
      "endTime": "16:00",
      "_id": {
        "$oid": "62d13383bd1ad85d85ae4b57"
      }
    },
    {
      "day": "Thursday",
      "startTime": "08:00",
      "endTime": "16:00",
      "_id": {
        "$oid": "62d13390bd1ad85d85ae4b5b"
      }
    },
    {
      "day": "Friday",
      "startTime": "08:00",
      "endTime": "16:00",
      "_id": {
        "$oid": "62d13396bd1ad85d85ae4b5f"
      }
    }
  ],
}

I am sending following request from the postman to get data:

{
    "city":"edmund",
    "startTime": "2022-07-25 11:00",
    "duration":{
        "days":"0",
        "hours":"3",
        "minutes":"00"
    }
}

Now, the thing to note here is that 25-july 2022 is Monday and I want to show data of Monday only. I don't want to show data of availability from Tuesday to Saturday like first code block. the other thing is if duration exceeds 24 hours then want to show availability of monday and tuesday only.

How can I do that?

below is the route to fetch availability:

exports.getParkingListByCriteria = async (req, res) => {
  try {
    endTime = getEndTime(req.body.startTime, req.body.duration);
    const startTime = dayjs(req.body.startTime, "YYYY-MM-DD HH:mm").format("dddd");
    let parkings = await Parking.find({
      "location.city": req.body.city},
      { "availability.day": { $elemMatch: { day: startTime } } } //I have tried this but it is still returning all the availability from monday to saturday and even hardcoded text "Monday" returns all availability data
    );
    parkingList = [];
    parkings.filter((parking) => {
      if (isParkingAvailable(parking.availability, req.body.startTime, endTime))
        parkingList.push(parking);
    });
    res.status(200).send(parkingList);
  } catch (error) {
    return res.status(500).json({ error: error.message });
  }
};
Talc
  • 109
  • 6

1 Answers1

0

Ok, I Found the solution for one particular problem. I was doing it wrong. The solution:

Parking.find({
        "location.city": req.body.city },
      {
        availability: { $elemMatch: { day: startTime } },
        totalSpots: 1,
        contactInfo: 1,
        ...
      })

But still I have 1 problem and that is If duration exceeds more than 24 hours than it is not showing data of Tuesday instead it is returning an empty array

Talc
  • 109
  • 6
  • This appears related to the other question? – Buzz Moschetti Jul 18 '22 at 19:44
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 20 '22 at 04:43