I have the following query which returns user documents that match the search term
provided by the requested user:
// user who made request
const user = await User.findOne({_id: req.user._id})
// return users that match the search query provided by requested user
const usersQuery = await User.aggregate([
{
$match: {
$and: [
{ username: { $regex: new RegExp(query, "i") } },
],
},
},
{
$project: {
// This field projects whether the requested user is following this particular user
isFollowing: { $in: ["$_id", user?.following] },
username: 1,
privacyStatus: 1,
},
},
])
The isFollowing
property projects whether the requested user is following the particular user which was returned by the aggregation query.
If the isFollowing
property is true
I would like to add a $lookup
stage to the aggregation which returns all the posts
that particular user has created. How would I implement this $lookup
stage so that it is only performed if isFollowing
is equal to true
? Thanks.