const pipeline = [ { $match: { type: 'post' }, }, { $match: { owner: { $in: ['1', '2', '3'] } }, }, { $match: { name: new RegExp(`.*${q}.*`, "i"), }, }, { $sort: { _id: -1 }, }, { $limit: 40, }, { $lookup: { from: "users", localField: "_id", foreignField: "user_id", as: "user", pipeline: [ { $match: { role: "manager" }, }, { $lookup: { from: "comments", localField: "_id", foreignField: "user_id", as: "feedbacks", pipeline: [ { $match: { type: "feedback" }, }, ], }, }, ], }, }, ]; module.exports = { pipeline };