Note that there are some explanatory texts on larger screens.

plurals
  1. POTwitter-like app using MongoDB
    primarykey
    data
    text
    <p>I'm making an app that uses the classic "follow" mechanism (the one used by Twitter and a lot of other apps around the web). I'm using MongoDB. My system has a difference, though: an user can follow <em>groups</em> of users. That means that if you follow a group, you'll automatically follow all the users who are members of that group. Of course users can belong to more than one group.</p> <p>This is what I came up with:</p> <ul> <li>when <strong>user A</strong> follows <strong>user B</strong>, id of user B gets added to an embedded array (called <code>following</code>) in user A's document</li> <li>for unfollowing, I remove the id of the followed user from the <code>following</code> array</li> <li><p>groups work in the same way: when <strong>user A</strong> follows <strong>group X</strong>, id of group X gets added to the <code>following</code> array. (I actually add a <code>DBRef</code> so I know if the connection is to an user or a group.) </p></li> <li><p>when I have to check if <strong>user A</strong> follows <strong>group X</strong>, I just search for the group's id in <strong>user A</strong>'s following array.</p></li> <li>when I have to check if <strong>user A</strong> follows <strong>user B</strong>, things gets a little trickier. Each user's document has an embedded array listing all the groups the user belongs to. So I use an <code>$or</code> condition to check if user A is either following user B directly or via a group. Like this: <blockquote> <p><code>db.users.find({'$or':{'following.ref.$id':$user_id,'following.ref.$ref','users'},{'following.ref.$id':{'$in':$group_ids},'following.ref.$ref':'groups'}}})</code></p> </blockquote></li> </ul> <p>This works fine, but I think I have a few issues. For example how do I show a list of followers for a particular user, including pagination? I can't use skip() and limit() on an embedded document.</p> <p>I could change the design and use an <code>userfollow</code> collection, which would do the same job of the embedded <code>following</code> document. The problem with this approach, which I tried, is that with the <code>$or</code> condition I used earlier, users following two groups containing the same user would be listed twice. To avoid this I could use group or MapReduce, which I actually did and it works, but I'd love to avoid this to keep things simpler. Maybe I just need to think out of the box. Or maybe I took the wrong approach with both tries. Anyone already had to do a similar thing and came up with a better solution? </p> <p>(This is actually a follow-up to <a href="https://stackoverflow.com/questions/3838466/making-a-twitter-like-timeline-with-mongodb">this older question</a> of mine. I decided to post a new question to explain my new situation better; I hope it's not a problem.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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