Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Twitter-like timeline with MongoDB

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?

like image 285
ySgPjx Avatar asked Oct 01 '10 10:10

ySgPjx


2 Answers

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.

like image 124
Sinan Avatar answered Sep 19 '22 13:09

Sinan


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

like image 29
sdot257 Avatar answered Sep 20 '22 13:09

sdot257