How to use index in $group with $sum then $sort

You’re probably on the cusp of in-memory there, if you filter it down a bit (with a $limit or something) how much do you need to reduce to not have the issue?

With the $sort added and using allowdiskuse, does that calculate in enough time for your use case?

At this point I guess it’s down to if you think each time you call this query, is it reasonable to take the hit of this query, if the data only updates on occasion then you could pre-calculate it when making changes, or just do it every so-often.

There have been a few questions about grouping large datasets recently and there gets to a point with mongo where some things just take a little while, it may be quicker on some RDMS servers but mongo gives you other flexibility!

You could do a merge update with this, so when you run it, it works out the most used and updates the document that matches.

You could store the data as arrays with a document for part number…but you need to be wary in case you have a monster product that has a million components and you’ll blow the doc size limit (16MB).
I’m sure running a $size and getting the biggest would be pretty quick.