Hi @Golu_Rajak
Looking at your example and your aggregation, I’d remove the grouping and unwind. I say this because your grouping is essentially transforming the result, which can be done in the pipeline of the lookup, then you just need to project the property back to an array. Below is the aggregation rewritten (slightly) for mongocompass:
[
{
$match: {
_id: ObjectId('64d1377633b2c02fd6eaa1b3'),
},
},
{
$lookup: {
from: "posts",
localField: "posts.post",
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$status", "active"],
},
{
$lt: [
"$expiry_time",
new Date(),
],
},
],
},
},
},
{
$project: {
_id: 1,
caption: 1,
},
},
],
foreignField: "_id",
as: "posts.post",
},
},
{
$project: {
name: 1,
posts: {
$slice: ['$posts.post', 0, 20],
},
},
}
]
Using your sample data this gives the result:
{
"_id": {
"$oid": "64d1377633b2c02fd6eaa1b3"
},
"name": "darkness",
"posts": []
}
Which I believe is what your are after. Adding the status to active on one of you post documents you will get the following:
{
"_id": {
"$oid": "64d1377633b2c02fd6eaa1b3"
},
"name": "darkness",
"posts": [
{
"_id": {
"$oid": "659edd406d831d0eda4a6b6f"
},
"caption": "power"
}
]
}
Hope that helps.
Craig.