steevej
(Steeve Juneau)
4
First, a big thank you for providing a playground. It makes helping you so much easier to do.
As mentioned by John
and $group. Group is expensive in terms of memory as all documents must be processed before the first document is output.
So in a case like this, where I assume that you have a collection of unique user profile, I would start the aggregation with the user_collection rather than the order_collection. You already have all the groups, you then $lookup the order_collection to get what you want.
The first stages would looked like the one at this updated playground where I added a user_collection. With an index on users.name the $lookup might be faster than $unwind/$group.
The stages to count the unique items would be a bit more complicated due to the different schema between bulk and individual. The $lookup pipeline probably could be use to $set items with $cond of order_type, for bulk $items is used as-is while for indivual would use the items specific for the user.
I’ll come back here when I have an idea and time.
3 Likes