MongoDB: How to design twitter-style followers/following relation model in MongoDB? MongoDB: How to design twitter-style followers/following relation model in MongoDB? mongodb mongodb

MongoDB: How to design twitter-style followers/following relation model in MongoDB?


1. collection users:- userid- username- userpass- other user specific info user2. collection following:- userid- [array of followingid]3. collection followed:- userid- [array of followedid]4. messages_relation collection:- userid- messageid- time5. messages_text:- messageid- text


I'd start by reading this documentation on storing comments in a CMS if you haven't already. While it's for comments, the same general problem exists -- where you can't store all comments in a single document (in your case, followers/following).

Either the Hybrid approach (which uses fewer docs and stores some number of relations inside of a single doc) or the approach you're describing should work well.

I'd also suggest building a simple POC to test the performance of retrievals and such. It might make sense to cache some results, or precompile them. Usually, it's OK in systems like this if everything isn't instantly consistent for all users (like having the follower count be correct right away).

There likely isn't a perfect solution, and may require a few solutions for optimal performance (like how a user and followers are handled may change as the number of followers increases for example).


you may want to check flockDB a database that stores adjancency lists. https://github.com/twitter/flockdb