Best index on aggregation with multiple match conditions

One thing that occurred to me is that you could simplify your pipeline a little to only $match on __STATE__ and then group on countryCode and then filter out _id being null. Filtering out records where countryCode is null or missing may be taking a lot more time/effort before $group (with minimal reduction in total records processed) when it would be very fast after…

Asya