MongoDB document design for comments (and their comments)

I have a model that looks like this:

class Comment { public string ID { get; set; } public string ArticleType { get; set; } public string ArticleID { get; set; } public string Body { get; set; } public DateTime DateCreated { get; set; } public string UserID { get; set; } } 

I am creating an application for storing comments about other materials in our application. For example, if the comment refers to a product, ArticleType may be a "product" and ItemID will be the identifier of the product ...

I am going to use mongodb to store this data

I want to be able to respond to a comment and store the answer hierarchically Should I store the list in a doc comment?

I read this article by Ash Ashton. What makes sense for things like a blog post, and his comments ...

However, in my model, “response comments” refer directly to the parent comment.

Response comments may also contain answers, making them x levels ...? Could this be due to the scale of the query type of type reduction?

Edit:

"article" is probably bad terminology. ArticleType and ArticleId were just a way to link a comment to a specific “thing” - for example, if we were to comment on this question, articleType could be stackOverflowQuestion and id would be 5144273

If we commented on the ebay auction, we could have articleType as ebay and articleId as 1234902493984 (item number)

Hope this makes sense ...

+2
source share
2 answers

I see three possible solutions (this is just my opinion):

one.

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

Pros: you can easily load, display hierarchical data. You do not know the parent comment from the comment.
Cons: you cannot request and update a comment with some id.

2.

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

Pros: You can request / update as you wish.
Cons: A large number of requests to mongo when you need a load hierarchy.

3.My favorite;):

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

Pros: You can request / update as you wish. You can download the article with all the comments on one request. No need to store redurant ArticleType and ArticleId
Cons: You must download the article and create a hierarchy in memory.

I hope this help helps you make a choice.

+1
source

Response comments may also contain answers, making them x levels ...?

So, if you want to simulate this, an easy way to do this is to give each comment a list of comment properties. This gives you a natural hierarchical structure that matches the data.

Will it be because of the volume of the request for a reduction in the type of card?

Not. The Map function is just a javascript method. It can call other methods, it can walk through a tree recursively.

Your model:

One thing that bothers me about your proposed model is that you have an identifier and ArticleID and ArticleType? How exactly do you plan to access this object? What types of indexes did you plan?

Will there be access to comment information outside the scope of the article? Are you going to upload comments using the "article"? Does it make sense to just store the List<Comments> inside the article itself?

There are several ways to model this data. So it really will depend on what you want to get out of. You cannot optimize all possible queries. If you can tell us what queries and use cases that you want to make quickly, then it’s easier for you to provide recommendations for data modeling.

+1
source

All Articles