0

I have a huge experience with MySql and Oracle as relational DBs, but I'm very confused about how properly create collections in MongoDB.

I was reading so many articles and watching youtube videos, but didn't get any real example of how properly create the structure and relationship between two or three collections (properly, best practice or whatever...)

For example... Assume we have three collections Users, Comments, and Posts. What will be right design to use? If the Comments is embedded inside of Posts, then what I have to do in case an User changed his name? Should I run through all comments related to a post in order to update his name in Comments collection? If it's a referenced one, then how to fetch data from all three collections (Post->Comment->User)... Aggregation? If it does, then how MongoDB will behave if the collection will grow up and reach, let's say, 100,000 documents...

Well... I hope you've got my point.

I'll be glad if you guys will post your comments and thoughts about all this stuff and "clarify" all this.

Tnx.

Eugene Vilder
  • 492
  • 1
  • 9
  • 26

2 Answers2

0

You are still thinking SQL, there are no 3 collections, just one or two if the number of posts is huge and so is the number of comments. Let's look at the Posts as a collection. Post is created by user and so are the comments (different users). User will not change his name that often if at all and when one does just run an update. { _id : PostID, title: string, body: string, meta: {...}, user: { id : UserId, name: string }, comments: [ { id: CommentId, by: { id : UserId, name: string } }, ... ] } If a user will change his name then you run two updates. One for owner and one for comments using positional operator. Personally I don't think this will happen often. If the number of posts is huge and they are active with comments and such, then one can think about 2 collections one for posts and one for comments or consider sharding but not as a first option. 100K is not a very big number at all.

Tal G.
  • 483
  • 5
  • 15
  • Tnx Tal for the comment. I was thinking about this approach, but... Don't you think to add UserId and his name to every single comment are redundant? Since I'm using Mongoose in my project how about using a Populate function? Have you ever had an experience with this function? – Eugene Vilder Jun 17 '17 at 15:44
  • There are no joins in mongo. Don't solve this by application joins and don't try to save space in your document. It's much cheaper, application wise to pull everything in one document. That said you need to think and design your project properly and perhaps mongo is not the best tool for this job. – Tal G. Jun 17 '17 at 18:16
0

I think you can embed comments with user ID inside Post, then used a separate doc of userid inside post for collecting all users contribute to the post (comments etc). Since userid is key, when you return post you can flat taht doc with user's info. Then on client side you can use user document with names etc to recreate the user name etc with the post and comments.

  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please [edit] to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. – Toby Speight Jun 19 '17 at 15:03