User-report test - am I bulding an unoptimized pipeline?

So here is the basic idea on how my pipeline works:

  1. $lookup to do a JOIN on users and their respective comments, the comments’ array is named “comments”

  2. $project the size of the “comments” array for each JOINed document, the field is called “count”

  3. $sort the documents by count in descending order

  4. $limit to 20 documents

But this actually takes WAY too much time to execute, both in Compass and in the npm tests, causing a timeout even if it’s set for 200 seconds. Is there a more efficient way to build the pipeline? I’ve been stuck in this test for 2 hours now.

is this to get top 20 users with most comments? If so, then you dont need any JOINs. All you need is to find all the comments of each user then sort them in descending order and get the top 20.

HINT: $sum

2 Likes

Thanks a lot! I managed to solve it.

If anyone has the same problem, I solved it by using the $group aggregation and - as hotdog 1987 said - by creating a count field that was the $sum of the total of documents found per user.

1 Like