I have the following schema(s):
Customer
{
_id: "...",
name: "...",
email: "...",
}
Hotel:
{
_id: "...",
name: "...",
reviews: [
{
_id: "..",
rating: 1, // number
content: "...",
customerId: "..." // ObjectId ref to customer
},
{
_id: "...",
rating: 1, // number
content: "...",
customerId: "..." // ObjectId ref to customer
}
]
}
For simplicity I cut off most of the properties from both models
I am trying to use $lookup to fetch customer data using customerId which is present in the review object within the reviews array, I tried:
Hotel.aggregate(
[
{
$lookup: {
from: "customers",
localField: "reviews.customerId",
foreignField: "_id",
as: "customer"
}
}
]
)
But it is not working.
How do I lookup customers using the customerId present in the review object within the reviews array?