I have this dataset:
{
name: "Lakawon Island Resort",
price_per_night: 2804.0,
address: "Cadiz Viejo",
amenities:
{
tv: false,
reservation: false,
},
reviews: [
{
_id: 03020,
user: 1030340,
comment: 'Hi there'
}
]
image:
"https://images.unsplash.com/photo-1591017403286-fd8493524e1e"
},
And here is my Schema:
const reviewSchema = mongoose.Schema({
rating: {
type: Number,
required: true
},
comment: {
type: String,
required: true
},
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
}, {
timestamps: true
})
const resortSchema = mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
name: {
type: String,
required: true
},
price_per_night: {
type: Number,
required: true,
},
address: {
type: String,
required: true
},
reviews: [reviewSchema],
image: {
type: String
},
amenities: {
tv: { type: Boolean, required: true, default: false },
reservation: { type: Boolean, required: true, default: false },
moderate_noise: { type: Boolean, required: true, default: false },
},
}, { timestamps: true
})
const Resort = mongoose.model('Resort', resortSchema)
I am trying to pull up the list of array of object of users inside the reviews array which is also inside the resorts list. So far I got this which did not work:
const reviews = await Resort.findById(req.params.id).populate({ path: 'resort.reviews', populate: { path: 'reviews.user' } })
I want to seek for help on how to attain this. Any idea so I'll display something like this on my api?
{
name: "Lakawon Island Resort",
price_per_night: 2804.0,
address: "Cadiz Viejo",
amenities:
{
tv: false,
reservation: false,
},
reviews: [
{
_id: 03020,
user: {
_id: 03030,
name: 'Rick Martin'
},
},
{
_id: 03021,
user: {
_id: 04031,
name: 'Rick Martin'
},
}
]
comment: 'Hi there'
}
]
image:
"https://images.unsplash.com/photo-1591017403286-fd8493524e1e"
},