Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way you want to do it is the right way. One of advantages to something like MongoDB is that you can embed data to reduce the queries you need to run on a page. Which is what you're doing by caching the followers count.</p> <p>Both of your options work, it's just about the tradeoffs you want to make.</p> <p>1) Less efficient, but the most accurate. Any sort of desync that somehow occurs will be fixed when they next follow someone. This is a decent way to start off and then change it later if the follower counts or the user database gets large.</p> <p>2) Most efficient, you need to make sure you always call $inc when changing the follower count or it'll become desynced until you do #1.</p> <p>Alternatively, you can do #2 and then run a reconciliation job in the background that will resync it on the offhand chance it does become desynced. You can do this easily with the <a href="http://docs.mongodb.org/manual/core/aggregation/" rel="nofollow">MongoDB aggregation framework</a>, you can speed up the reconciliation job by using a timestamp in the <code>User</code> collection and only recalculating the followers for those who were updated since the last time the job ran.</p> <p>To expand as the original question was updated:</p> <p>In general, if you can avoid doing a <code>count()</code>, you should.</p> <p>Nothing is wrong with starting off using <code>count()</code> and then swap to a more efficient solution (#2) later on. Caching the <code>count()</code> results somewhere like memcache can also be used to reduce performance issues until you stop using <code>count()</code> directly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload