Search code examples
node.jsexpressmongoosemongoose-populate

Mongoose populate return [Object] instead of actual data of the field


The availability array from the parking model. I am querying the booking model which has parking Id.

Following is the query:

let startTime = await Booking.findOne(
      { bookingId: req.params.id },
      { startTime: 1, _id: 0 }
    ).populate({
      path: "parkingId",
      select: ["availability"],
    });

It gives the following result:

{
  parkingId: {
    _id: new ObjectId("62e11ab3079daa939290fa07"),
    availability: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
  },
  startTime: 2022-07-26T09:30:00.000Z
}

I want to populate the availability's endTime field. how can I do that? See, it is an array of Objects.

The availability array example:

[
    {
      "day": "Monday",
      "startTime": "09:00",
      "endTime": "17:00",
    }... //Other days
]

Solution

  • I just found the solution which is as following:

    bookingData = await Booking.findOne(
          {
            bookingId: req.params.id,
          },
          { startTime: 1, _id: 0 }
        ).populate("parkingId");
    
        let parkingAvailability = bookingData.parkingId.availability;
    

    Now parkingAvailability stores the availability.