'populate all array of objects
I have a schema of concerts which I am populating with reviews using virtual populate which returns an array of reviews. Now on every review I again have to populate customerId to its name and avatar.
Here is my code
const populateReviews = await Concert.findById(id).populate({
path: "reviews",
options: {
sort: { rating: -1, updatedAt: -1 },
},
});
const populateCustomers = await Customer.populate(populateReviews, {
path: "reviews.customerId",
options: {
select: { name: 1, avatar: 1 },
},
});
And here is the output of above code.
I have to populate customerId. Please help me with this.
Solution 1:[1]
Populating across multiple levels would help you
Concert
.findById(id)
.populate({
path: "reviews",
options: {
sort: { rating: -1, updatedAt: -1 },
},
populate : {
path : 'customerId',
options: {
select: { name: 1, avatar: 1 },
},
}
})
.exec(function (err, res) {
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ehsan Shavandi |

