MongoDB document design for comments (and their reply comments)

≯℡__Kan透↙ 提交于 2019-12-04 16:47:06

I see three posible solutions(it just my opinion):

1.

public class Comment 
{
  ...
  public List<Comment> ChildComments {get;set;}
}

Pros: you can easy load, display hierarchical data. you don't know parent comment from comment.
Cons: you can't query and update comment with some id.

2.

public class Comment 
{
  ...
  public string ParentCommentId {get;set;}
}

Pros: You can query/update as you want.
Cons: Big amount of requests to mongo when you need load hierarchy.

3.My favorite one ;) :

 public class Comment 
 {
   ...
   public string ParentCommentId {get;set;}
 }

 public class Article
 {
   ...
   public List<Comment> Comments {get;set;}
 }

Pros: You can query/update as you want. You can load article with all comments in one request. No need to store redurant ArticleType and ArticleId
Cons: Need to load article and build hierarchy in memory.

Hope this help you make choice..

Comment replies could also even have replies, making them x levels deep...?

So if you wanted to model this, the easy way to do this is to give each comment a list of Comments property. This gives you a natural hierarchical structure that fits the data.

Would this be out of the scope of map reduce type query?

No. A Map function is just a javascript method. It can call other methods, it can recursively walk a tree.

Your model:

One thing that worries me about your proposed model is that you have an ID and an ArticleID and an ArticleType? How exactly are you planning to access this object? What type of indexes were you planning?

Is the comment information going to be accessed outside the scope of the article? Are you going to load the comments with the "article"? Does it make sense to just store a List<Comments> inside of the article itself?

There are several ways you could model this data. So it's really going to depend on what you want to get out. You can't optimize for every possible query. If you can tell us which queries and use cases you want to make fast, then it's easier to provide advice on modeling the data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!