Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB performance: newsfeed architecture, subscribers, comments
    primarykey
    data
    text
    <p>I use MongoDB + PHP for a "facebookish" newsfeed with different kinds of feeds (post, photo, poll, etc.) and with comments.</p> <p>Each feed belongs to some "channel" - currently it could be <em>user</em> or <em>group</em> (there may be more containers in future).</p> <p>Any user can subscribe for any channel or unsibscribe from it.</p> <p>Now let's say there're tons of channels and tons of feeds. What is the best structure for channels/feeds/comments?</p> <p>I'm thinking about two approaches:</p> <p><strong>1) Feeds collection with list of subscribers in each feed:</strong></p> <pre><code>feeds: [ {date_added: ..., last_update: ..., title: ..., text: ..., channel: ..., channel_subscribers: [...], comments_subscribers: [...], comments: [...] }, {...}, {...}, {...} ] </code></pre> <p>If I want to get last feeds: </p> <pre><code>db.feeds.find({date_added: "this week", channel_subscribers: "my_login"}); </code></pre> <p>If I want to get feeds with new comments:</p> <pre><code>db.feeds.find({last_update: "this week", comments_subscribers: "my_login"}); </code></pre> <p><em>Pros:</em></p> <ul> <li>Simple and fast readings?</li> </ul> <p><em>Cons:</em></p> <ul> <li>When I want to subscribe/unsibscribe for/from a channel, I have to run trough all feeds and push/pull my name from list of channel_subscribers; it could be slow if I have tons of feeds</li> </ul> <p><strong>2) Separate "channels" collection:</strong> Same thing but keep list of subscribers in channel collection: </p> <pre><code>channels: [ {channel_id:..., last_update: ..., subscribers: [...]}, {channel_id:..., last_update: ..., subscribers: [...]} ] </code></pre> <p>First I have to query last updated channels:</p> <pre><code>subscribes = db.channels.find({last_update: "today", subscribers: "my_login"}) </code></pre> <p>Now find my feeds:</p> <pre><code>db.feeds.find({channel: {$in: subscribes}], date_added: "today"}) </code></pre> <p><em>Pros:</em></p> <ul> <li>Simple, fast and more safe subscribing/unsubsribing;</li> </ul> <p><em>Cons:</em></p> <ul> <li>I feel I should avoid <strong>$in</strong> because it's slow(?), especially when I have lots of subscribes to put inside of this operator.</li> </ul> <p><strong>3) Keep user subscribes in users collection (so each user has an array of his own subscribes)</strong> </p> <pre><code>users: [ {_id: ..., login: ..., email: ..., subscribes: [...]} ] </code></pre> <p><em>Cons:</em> - in this case we'll have even bigger array to put inside of $in than in previous (#2) approach.</p> <p><strong>4) Your suggestions?</strong></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.
 

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