Trying to filter sub document in aggregation but in response not getting parent data if sub document data is empty getting only array.
const data = await item.aggregate([
{
$match: {
_id: new Types.ObjectId(id),
},
},
{
$lookup: {
from: "posts",
localField: "posts.post",
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$status", 'active'],
},
{
$lt: ["$expiry_time", new Date()],
},
],
},
},
},
],
foreignField: "_id",
as: "posts.post",
},
},
{
$unwind: "$posts.post",
},
{
$group: {
_id: "$_id",
name: { $first: "$name" },
posts: {
$push: {
_id: "$posts.post._id",
caption: "$posts.post.caption",
},
},
},
},
{
$project: {
name: 1,
posts: {
$slice: ['$posts', (+page - 1) * 20, 20],
},
},
},
]);
if the condition satisfy any post then I get parent data other wise I get an empty array.