0

I'm here to get some advice about the best way of building my data model (mongodb) in my application.

Let's say I will have those entities :

User: A User with all the usual attributes and I need each User to have a list of followers and following. Each User will also have a list of "Post" that he find interesting.

Post: A post will have a content, a creation date, an author (user), a list of User that likes the post (I'd like to be able to retrieve user informations from a post), a list of comments (comment)

Comment: A comment will have an author(User), a content, and has to be linked to a post.

I'm thinking this application to handle lots of content and users, that's why i'm really concerned about this data model, and I'd like to be able to build it the best way in term of maintainability and perfromance.

I really can't decide by my own in which case I need to use the embbeded document capabilities or to reproduce a relationnal like data model.

How would you build this data model ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

We are in the process of migrating our posts from MySQL to MongoDB so I have considered these same things.

We decided to keep users and posts in separate collections and embed only the properties that we are interested in each way. So for example on a post we have an array of users who like that post, but each element in the array has only the name and username of the user. When displaying the posts we can display the list of names of users who like the post and generate a link to each of them using the username.

The thing which needs to be considered is if a user changes their name or username then you need to update every post that they created or liked. We don't allow usernames to change, and have decided that it is not necessary in our case to update the names.

The issues in terms of performance with this approach are the padding which MongoDB automatically adds to the documents to allow them to grow, and the overhead of moving the document if it grows past this space allocated to it. Our system is much heavier on reads than writes for feed posts so these are things that we are happy to deal with.

The correct way to design your schema in MongoDB ultimately depends on how you access your data, and your specific use cases. We are also just getting started, but this approach is what makes sense to us right now.

Brett
  • 3,825
  • 3
  • 24
  • 27
  • How did you proceed to restrict the content of the array at just the name and username of a User ? – user2865790 Oct 10 '13 at 09:50
  • We manually push an object with just the name and username onto the array when a user likes a post. – Brett Oct 10 '13 at 10:16
  • Ok. I asked this because I believe that I've seen a way to do that kinda automatically with mongo. But i can't find it again. – user2865790 Oct 10 '13 at 10:54