I have 2 collection:-
First one is plan collection
{"_id":{"$oid":"615d54ecd0d7763ba6c05819"},"h2":["6040e39b7429ab001221df6f_6040e39b7429ab001221df70_6040e39b7429ab001221df71_6040e39b7429ab001221df72_6040e39b7429ab001221df73"],"isArchive":false,"isCreateTeam":false,"dueDate":{"$date":"2021-10-28T00:00:00.000Z"},"endDate":{"$date":"2021-11-30T00:00:00.000Z"},"startDate":{"$date":"2021-10-30T00:00:00.000Z"},"name":"EX_PLAN","linkedBrief":"5feb2ecc63f7cb001292c80b","totalBudget":12345,"totalPlanned":0,"buyerAccepted":0,"status":"inDraft","h1":"6040e35522b8b80012668c80_6040e35522b8b80012668c81_6040e35522b8b80012668c82_6040e35522b8b80012668c83","brandLevel":"6040e39b7429ab001221df6f_6040e39b7429ab001221df70_6040e39b7429ab001221df71_6040e39b7429ab001221df72_6040e39b7429ab001221df73","teamSize":"large","marketCode":"GB","currencyCode":"GBP","uid":"dev_000008","clientCode":"dev","tenantId":"dev","createdBy":"00uanynrxPgBNVKsr416","originalId":"615d5474d0d7763ba6c057dc","createdAt":{"$date":"2021-10-06T07:47:00.849Z"},"updatedAt":{"$date":"2021-10-06T07:49:00.274Z"},"updatedBy":"00uanynrxPgBNVKsr416","__v":0}
and second one is briefs collection which are linked to the plans:-
{"_id":{"$oid":"615d54ecd0d7763ba6c05817"},"activeUserType":["planner"],"buyers":[],"image":"","isRecalled":false,"owner":"planner","createdBy":"00uanynrxPgBNVKsr416","buyerAccepted":0,"h3":"6040e4567429ab001221df82_6040e4567429ab001221e09e_6040e4567429ab001221e09f","name":"Digital","plan":{"$oid":"615d5474d0d7763ba6c057dc"},"status":"inDraft","totalPlanned":12345,"originalId":"615d5474d0d7763ba6c057de","clientCode":"dev","tenantId":"dev","createdAt":{"$date":"2021-10-06T07:47:00.861Z"},"updatedAt":{"$date":"2021-10-06T07:49:00.258Z"},"updatedBy":"00uanynrxPgBNVKsr416","currentPlanned":12,"__v":0}
where First collection have original Id present in the form of plan key in second collection.
and here is the aggregation:-
ChannelGroupData is equivalent to Briefs
[
{
'$match': {
'deletedAt': {
'$exists': false
},
'linkedBrief': {
'$exists': true
}
}
}, {
'$addFields': {
'planOriginalId': {
'$toObjectId': '$originalId'
},
'id': '$originalId'
}
}, {
'$lookup': {
'from': 'Briefs',
'let': {
'planObjectId': '$planOriginalId'
},
'pipeline': [
{
'$match': {
'$expr': {
'$eq': [
'$plan', '$$planObjectId'
]
},
'deletedAt': {
'$exists': false
},
'cloneId': {
'$exists': false
}
}
}
],
'as': 'channelGroupData'
}
}, {
'$unwind': '$channelGroupData'
}, {
'$group': {
'_id': {
'id': '$id',
'linkedBrief': '$linkedBrief',
'currencyCode': '$currencyCode'
},
'channelGroupData': {
'$push': '$channelGroupData'
},
'channelGroupsForFilter': {
'$push': '$channelGroupData'
}
}
}, {
'$project': {
'channelGroupData': {
'_id': 1,
'name': 1,
'totalPlanned': 1,
'status': 1
},
'channelGroupsForFilter': {
'status': 1
}
}
}, {
'$addFields': {
'status': {
'$cond': {
'if': {
'$setEquals': [
{
'$setIntersection': [
'$channelGroupsForFilter', [
{
'status': 'complete'
}
]
]
}, '$channelGroupsForFilter'
]
},
'then': 'complete',
'else': 'inDraft'
}
}
}
}, {
'$project': {
'_id': 0,
'id': '$_id.id',
'linkedBrief': '$_id.linkedBrief',
'currencyCode': '$_id.currencyCode',
'channelGroupData': {
'_id': 1,
'name': 1,
'totalPlanned': 1
},
'status': 1
}
}
]
Aggregation takes more than 4 sec. Please some one help to optimize this aggregation.