Modeling a NoSQL Forum Application Using C # / ASP.net MVC

I am currently developing a forum-based application (question / answer).
Using C # ASP.net MVC and MongoDB to store data.

I'm looking at a model now.
I was thinking that I have separate classes: (simplified)

public class Question { public string ID { get; set; } public string Title { get; set; } public string Body { get; set; } public List<string> Tags { get; set; } public DateTime DateCreated { get; set; } public string ForumID { get; set; } } 

Answer

 public class Answer { public string ID { get; set; } public string QuestionID { get; set; } public string Body { get; set; } public DateTime DateCreated { get; set; } } 

My questions:
How to handle "responses"
It’s best to have (as in the model above) two separate β€œentities”
Or should I have a list of answers in my question model?

Some requirements are that I will need to display the number of answers, etc.

With this stored in NoSQL db, I know that I have to denormalize things, but how can I insert a response without receiving the whole message? Is such an operation possible using NoRM with MongoDB?

+3
source share
2 answers

Normally in MongoDB you should embed the answers inside the question. 99% of the time you are going to ask a question so that you can also get answers at the same time.

Some requirements are that I will need to display the number of responses ...

If you return answers to questions, it is very simple. You will have an array / list / collection with answers. So you just take the length.

but how can I insert an answer without getting the whole post

MongoDB supports the atomic operation "$ push". This means that you can add an element to the array without actually loading the document from the client. From a javascript shell, it will look like this:

db.questions.update( {_id : your_id}, { $push : { answers : your_answer_object } } );

So MongoDB is capable of this. You will need to check the NoRM drivers to make sure that they really allow this type of behavior (they really are missing something if they don't support $ push).

+6
source

The answer should be part of the question.

public class Question {public string ID {get; set; }

  public string Title { get; set; } public string Body { get; set; } public List<string> Tags { get; set; } public DateTime DateCreated { get; set; } public string ForumID { get; set; } public List<Answers> Answers { get; set; } } 

Due to the lack of document database associations, it is recommended to store copies of the entire chart in one document.

+2
source

Source: https://habr.com/ru/post/1333674/