Reproduction: https://mongoplayground.net/p/f9BraX4emor
The playground query does the job halfway. It finds the matching documents, but it doesn't aggregate bounties and emotes with a COUNT BY operation.
My desired result is this:
[
{
"_id": ObjectId("5e7d7bf7d86d85088863f4de"),
"channelId": "23435553",
"bounties": {
"user1": 150, // user1 had 2 bounties with 100 + 50 amounts
"user2": 200, // user2 had 1 bounty with 200 amount
},
"emotes": {
"A": 2, // There were 2 docs with emoteId == A
"B": 1, // There was 1 doc with emoteId == B
},
}
]
I actually don't care about the specific data structure of the aggregated result. For ex. I'd be fine if bounties and emotes were arrays instead of dictionaries. I think the $unwind operator can be used here, but I'm just not sure how.
Currently, I'm making way too many queries to fulfill a REST API request.
For ex, my API receives a request to find 10 suggestions for a channel. First, I run 1 query that returns 10 suggestions. Then FOR EACH suggestion, I run 1 query to aggregate bounties, and another to aggregate emotes. That's 21 queries...... in a single REST API call.. :(. I want to run 1 query, not 21.