Modelling a NoSQL Forum Application with C# / ASP.net MVC

时光毁灭记忆、已成空白 提交于 2019-12-04 13:55:41

问题


I'm currently developing a Forum (Question / Answer) based application.
Using C# ASP.net MVC and MongoDB for data storage.

I'm currently looking at the model.
I was thinking of having separate classes like this: (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 is:
How to handle the "replies"
Am I best of having (as in the model above) two separate "entities"
Or should I have a List of Answer in my Question model?

Some requirements are that i'll need to be able to display a count of answers etc...

With this being stored in a NoSQL db, I'm aware I should denormalize things, but how can I insert an answer, without retrieving the entire post? Is that sort of operation possible using NoRM with MongoDB?


回答1:


Normally in MongoDB, you would embed the answers inside the question. 99% of the time you're going to query by Question, so you might as well get the Answers at the same time.

Some requirements are that i'll need to be able to display a count of answer...

If you're bringing back the answers with the questions, this is really easy. You'll have an array/list/collection with answers. So you'll just grab the length.

but how can I insert an answer, without retrieving the entire post

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

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

So MongoDB is capable of this. You'll have to check with the NoRM drivers to ensure that they actually allow for this type of behavior (they're really missing something if they don't support $push).




回答2:


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; }
}

Because of the lack of joins document databases encourage you to store instances of the entire graph in one document.



来源:https://stackoverflow.com/questions/4278605/modelling-a-nosql-forum-application-with-c-sharp-asp-net-mvc

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