Yes thank you, i noted the above variable error and corrected ealier🙂. I did run the update and this is how the updated doc looks like:
{
"review": "Convallis turpis porttitor sapien ad urna efficitur dui vivamus in praesent nulla hac non potenti!",
"rating": 5,
"tour": "5c88fa8cf4afda39709c295d",
"user": "5c8a1dfa2f8fb814b56fa181",
"date": "2021-09-15T22:17:46.950Z",
"_id": "6142710bf9a3fdb5d8506f7a",
"__v": 0
}
However, I ran the aggregation with $match, $lookup and $project (with the updated reviews collection) but the result is an empty array:
Here is the route handler:
exports.getTourStats = async function (req, res) {
try {
const { id } = req.params;
const { ObjectId } = Types;
const match = { $match: { _id: ObjectId(id) } };
const lookup = {
$lookup: {
from: 'reviews',
localField: '_id',
foreignField: 'tour',
as: 'reviews',
},
};
const project = { $project: { reviews: 1, name: 1, _id: 0 } };
const stages = [match, lookup, project];
const tour = await Tour.aggregate(stages);
res.status(200).json({
status: `success`,
_id: id,
data: tour,
});
} catch (err) {
res.status(404).json({
status: 'fail',
message: err.message,
});
}
};
This is what I get as response from postman:
the $match and $project seem to work, but the &lookup stage is not and i dont understand why