How to get only matched data in node.js with mongoose? I am getting only partial data back

Can anyone help me out here?

I am building a bus ticket booking app in node.js. After running the GET query I am getting only data about Buses not the seats but I want seats data as well.

There are many different tables like bus-table, locations-table, route-table, booking-table and many other as well.

In this bus table an admin can enter the data about Bus like bus number and total seats. After that in the location and route table only admin can enter the data and below is the route schema:

const routeSchema = new mongoose.Schema(
  {
    Location: {
      from: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Location",
        required: true,
      },
      to: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Location",
        required: true,
      },
    },
    busId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Bus",
      required: true,
    },
    date: {
      type: String,
      required: true,
    },
);

Both from and to are referenced to same location table where two different collections are stored.

After that an authorized user can book tickets by selecting the route, bus and seats by providing necessary details. Here is the booking table schema:

const bookingSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true,
  },
  routeId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Route",
    required: true,
  },
  passengers: [
    {
      name: { type: String, required: true, trim: true },
      gender: { type: String, required: true, trim: true },
      age: { type: Number, required: true, trim: true },
    }],
  phone: {
    type: Number,
    required: true,
  },
  email: {
    type: String,
    required: true,
  },
  bookingDate: {
    type: String,
    required: true,
  },
  fare: {
    type: Number,
    required: true,
  },
  seats: {
    type: [Number],
    required: true,
  },
  departureDetails: [
    {
      city: { type: String, required: true, trim: true },
      location: { type: String, required: true, trim: true },
      time: { type: String, required: true, trim: true },
      date: { type: String, required: true, trim: true },
    },
  ],
  arrivalDetails: [
    {
      city: { type: String, required: true, trim: true },
      location: { type: String, required: true, trim: true },
      time: { type: String, required: true, trim: true },
      date: { type: String, required: true, trim: true },
    },
  ],
},{
    timestamps:true
});

But now the problem starts here in the GET request where I want to show every user of my app about the buses which will run on that route and seats available and reserved in every single bus running on that route. Booked seats are stored in the Booking table.

I am getting the bus data right and correctly but seats data is not showing. It is returning an empty array

Here is the GET request to get the bus and seats data:

router.get("/trip/single", async (req, res) => {
  if (!req.query.from || !req.query.to || !req.query.date) {
    return res.send({
      error: "Please enter the data to get the trip",
    });
  }
  const { from, to, date } = req.query;

  const routes = await Route.find({
    "Location.from": from,
    "Location.to": to,
     "date": date.toString(),
  });

  const matchedBus = await routes.filter(() =>{
    return Route.busId === routes._id
  });

  const bookings = await Booking.find({
    routeId: { $in: matchedBus.map((matchedBus) => matchedBus._id)  },
  });
  const busIdWithSeatsObj = {};
  
  for (let i = 0; i < matchedBus.length; i++) {
    let currentBusSeats = [];
    var busData = matchedBus.map(data => data)
    console.log(busData);
    **// Every thing is working fine till here. busData is returning the data as shown below**

    const busBookings = bookings.filter((booking) => {
      return (
        **//something is wrong here but I can not figure out what is wrong**
        booking.date === date.toString() &&
        booking.busId === matchedBus[i]._id
      );
    });
    console.log(busBookings);
       **//here busBookings is returning an empty array which is a big problem.**
    busBookings.forEach(() => {
      currentBusSeats = [...currentBusSeats, ...Booking.seats];
    });
    busIdWithSeatsObj[matchedBus[i]._id] = currentBusSeats;
  }

  res.status(200).send({ routes, matchedBus, busIdWithSeatsObj });
});

The console log of busData is:

[
  {
    Location: {
      from: new ObjectId("6295f0986f9e32990d8b3488"),
      to: new ObjectId("6295f0c06f9e32990d8b348b")
    },
    _id: new ObjectId("6295f12c6f9e32990d8b348e"),
    busId: new ObjectId("6295f0836f9e32990d8b3485"),
    date: '2022-06-02',
    departureTime: 11,
    arrivalTime: 6.3,
    createdAt: 2022-05-31T10:42:52.785Z,
    updatedAt: 2022-05-31T10:42:52.785Z,
    __v: 0
  }
]

I am passing the query like this:127.0.0.1:3000/trip/single?from=6295f0986f9e32990d8b3488&to=6295f0c06f9e32990d8b348b&date=2022-06-02

Now I am comparing the date and busId in the for loop which is not working I guess. What should I do to make it work and get the matched data?

Wrong forum.
Here we discuss about Mongoose OS.