Using a MEAN stack and Mongoose (5.9), please consider this model of a User document:
{
"_id" : ObjectId,
"Username" : "SamWhatever",
"Followers" : [ //may potentially grow to a huge number
ForeignObjectId,
ForeignObjectId,
ForeignObjectId,
...
],
"FollowersAmount": 20000001 //is it wise to substitute this by some native MongoDB functionality?
}
I am keeping track of followers in an array to possibly list all of them (via populate). On the profile page of a user, I want to display the amount of followers. Of course, I don't want to transmit the entire followers array but rather just send the plain amount of followers to the browser. I am not sure whether I should keep an additional FollowersAmount field in each user document which - of course - also requires further logic to always keep the value up-to-date or if I should rather use a native Mongoose functionality (which will also always be fast enough, irrespective of the future amount of items in that array) to count the amount of followers on-the-fly when querying a user and then append the result to the result set. Something like (pseudocode):
searchquery = User.findById({_id: id});
searchquery.populate({
count: 'Followers'
});
Any best practice advice what to use? Again, I just want to count the number of items in ONE array of ONE document, NOT ACROSS MANY documents. Besides, I expect the array Followers to grow to a huge number and would prefer a solution that scales.