I am trying to fix performance issue with mongodb and one of issue is that system read number of products in each category with every page load. Project uses C# and FluentMongo to access database. (and this wasn't my idea :)
Ideally, i want to get such categoryid : count of products as one request.
Each Product may belong to multiple Categories.
Code which I am trying to imporove is:
int count = _products.AsQueryable().Where(p => p.Categories.Any(c => c.Id == category.Id)).Count();
and it is executed hundred of times....