Campaign:
{“id”:1,“name”,“testcampaign”,“tags”:[]}
–tags field contaains list of References of linked tags collection document
Objective is to get list of all tags ,with the Count of Contacts & Campaigns who are referencing that particular tag.
I want to understand the best practice to do that through aggregation which is scalable and responds quickly as i am only interested in count of other collections.
An Example Result would be
[
{“id”:909,“name”:“newTag”,“campaignCount”:2,“contactCount”:5},
{“id”:909,“name”:“newTag”,“campaignCount”:0,“contactCount”:10}
]
Your expected result should match the supplied source documents.
We do not know how to generated 2 different counts from the same name:newTag.
We do not know how you refer to Tags from your tags: because you do not share what you have in the arrays. It could be id or name. But is it id or _id?
Hi Steeve,
I mistakenly typed same tag in expected results, and tried to edit,but there wasnt any option to edit.
Apologies.
Here is the Updated Sample Data and expected Result
1 - $lookup stage from:Contacts localField:_id foreignField:tags as:contacts
2 - $set stage to replace contacts with its $size
3 - 1 and 2 for Campaings
@steevej i have achieved the Step 1 and Step 2 . However i am not sure how to pass /use the result from the first aggregation in the second. any suggestions?
If your situation is different enough from the one from this thread that you cannot map the solution presented here it will be better if you start a new thread and post as much details as possible for your use case.