views:

71

answers:

2

What I need:

Suppose you're using MongoDB and you have a collection called users, and each user has a "following" array with user _ids of the people he's following. Then you have another collection statuses, with each status containing the _id of its author. How do you display to a certain user all the statuses added by people he's following?

What I tried:

I put all the users _ids that the current user is following in an array (I'm using PHP), then I used it to find all the statuses by those users using $in.

The question:

Is this the best solution?

+3  A: 

I can't see any other way too, i implemented such thing before and didn't have a problem.

On your case, it should be sth like this, you pass certain user's $follower_ids array as an argument to your function:

$query  = array("status_owner_id" => array('$in' => $follower_ids));
$cursor = $mongo->yourdb->statuses->find($query);

And if you index statuses (if you have enough ram to do so) upon owner_id you'd get the results really fast.

Hope it helps, Sinan.

Sinan Y.
+2  A: 

Yea, I do the exact same thing. See what Dwight Merriman suggested on his blog.

http://dmerr.tumblr.com/post/463694595/just-for-fun-a-single-server-twitter-design

luckytaxi