I'd like to know the best approach to do a count over a list that is part of a collection, for simplicity I've abstracted to the following model:
public class User
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<ObjectId> Followers { get; set; }
public List<ObjectId> Following { get; set; }
}
Just assume that probably the average of followers/ following could be around 2k on each property, I know some people like to pre-calculate the information using inc, is that recommend under the circumstances described above? if that's the case which could be the approach using the C# driver?
If you have a large number of users (say millions) and you query on this count frequently, why not use a map reduce? From the c# driver you could read the results of the map reduce.
If you have a small number of users (1000s), you could probably use the aggregation framework on demand, depending on how fast you need the query to be. See an example here Mongo order by length of array. From the c# driver you could execute the aggregation
If you decide to denormalize the count on saves, you can run a migration from the mongo shell to create the initial values via a cursor. See my answer about these types of migrations MongoDB: schema migration, update or insert