Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best MongoDB schema for twitter clone?

I know similar questions have been asked, but looking for a very basic answer to a basic question. I am new to MongoDB and making a twitter style app (blogs, followers, etc) and I'm wondering the best schema to use.

Right now I have (on a very high level):

Member {
  login: string,
  pass: string,
  posts: [
    {
      title: string,
      blog: string,
      comments: [ { comment: string } ]
    }
  ]
}

There is more to it, but that gives you the idea. Now the problem is I'm looking to add the "follow" feature and I'm not sure the best route to go.

I could add a "following" embedded doc to the Member, but I'm just not sure using mongoDB what the smartest method would be. My main concearn would obviously be the main "feed" page where you see all of the people you are following's posts.

like image 200
MrBojangles Avatar asked Jun 29 '11 04:06

MrBojangles


People also ask

How to design a good MongoDB schema design?

When you are designing your MongoDB schema design, the only thing that matters is that you design a schema that will work well for _your_ application. Two different apps that use the same exact data might have very different schemas if the applications are used differently. When designing a schema, we want to take into consideration the following:

How do I pull all my data together in MongoDB?

Now we can make one simple query to pull all that data together for our application. MongoDB schema design actually comes down to only two choices for every piece of data. You can either embed that data directly or reference another piece of data using the $lookup operator (similar to a JOIN).

What is document based design in MongoDB?

You can see that instead of splitting our data up into separate collections or documents, we take advantage of MongoDB's document based design to embed data into arrays and objects within the User object. Now we can make one simple query to pull all that data together for our application.

Is there a Twitter clone app for Django?

Twitter UI Clone built during a live stream. Twitter Clone | Social Network App in React.js and Firebase. Django ready-made web applications for various purposes. In this project, I built a Job hint application based on a redesign of Twitter.


1 Answers

This is not an ideal schema for a Twitter clone. The main problem is that "posts" is an evergrowing array which means mongo will have to move your massive document every few posts because it ran out of document padding. Additionally there's a hard (16mb) size limit to documents which makes this schema restrictive at best.

The ideal schema depends on whether or not you expect Twitter's load. The "perfect" mongodb schema in terms of maintainability and easy of use is not the same as the one I'd use for something with Twitter's throughput. For example, in the former case I'd use a posts collection with a document per post. In the high throughput scenario I'd start making bucket documents for small groups of posts (say, one per "get more" page). Additionally in the high throughput scenario you'd have to keep the follower's timeline up to date in seperate user timeline documents while in low throughput scenarios you can simply query them.

like image 140
Remon van Vliet Avatar answered Oct 04 '22 06:10

Remon van Vliet