简体   繁体   English

使用C#/ ASP.net MVC建模NoSQL论坛应用程序

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

I'm currently developing a Forum (Question / Answer) based application. 我目前正在开发一个基于论坛(问题/答案)的应用程序。
Using C# ASP.net MVC and MongoDB for data storage. 使用C#ASP.net MVC和MongoDB进行数据存储。

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? 将它存储在NoSQL数据库中,我知道我应该对事物进行反规范化,但是如何在不检索整个帖子的情况下插入答案呢? Is that sort of operation possible using NoRM with MongoDB? 使用NoRM和MongoDB可以实现这种操作吗?

Normally in MongoDB, you would embed the answers inside the question. 通常在MongoDB中,您可以将答案嵌入到问题中。 99% of the time you're going to query by Question, so you might as well get the Answers at the same time. 99%的时间你会按问题查询,所以你也可以同时获得答案。

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. MongoDB支持原子“$ push”操作。 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: 从javascript shell,它看起来像这样:

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

So MongoDB is capable of this. 所以MongoDB能够做到这一点。 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). 你必须检查NoRM驱动程序,以确保它们实际上允许这种类型的行为(如果它们不支持$ push,它们真的会丢失一些东西)。

Answer should be part of the question. 答案应该是问题的一部分。

public class Question { public string ID { get; 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. 由于缺少连接,文档数据库鼓励您将整个图形的实例存储在一个文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM