Mongo Aggregation to Calculate Unique IDs in Returned Docs is Slow
In addition to returning a filtered set of documents to the end user via a Mongo view, I also have a couple functions running to generate running totals. For some reason though, while my find()
operation is really fast (225ms), this additional aggregation I'm running takes over 6 seconds to perform -- which slows down the whole endpoint, because this data is passed on in what I'm returning.
I'm trying to understand why this aggregation would be so slow? This aggregation matches to the filters passed in by the end-user, and then calculates the number of unique customer ids that appear in the returned documents. This is what it looks like:
Why would this take 6 seconds to run? Any ideas? Any tips on how I can speed it up?
Well, it'll definitely take some time, if there's a lot of distinct users (or a lot users in general). If you want to check, why it's taking so much time, try the explain
argument.
On the other hand, distinct
might be a better choice here:
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.