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

发布于 2024-10-04 14:11:09 字数 841 浏览 0 评论 0原文

我目前正在开发一个基于论坛(问题/答案)的应用程序。
使用C# ASP.net MVC和MongoDB进行数据存储。

我目前正在查看模型。
我正在考虑开设这样的单独课程:(简化)

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 class Answer
 {
    public string ID { get; set; }
    public string QuestionID { get; set; }
    public string Body { get; set; }
    public DateTime DateCreated { get; set; }
 }

我的问题是:
如何处理“回复”
我最好拥有两个独立的“实体”(如上面的模型所示)
或者我应该在我的问题模型中有一个答案列表?

一些要求是我需要能够显示答案的数量等...

将其存储在 NoSQL 数据库中,我知道我应该对事物进行非规范化,但是如何在不检索答案的情况下插入答案整个帖子?使用 NoRM 和 MongoDB 可以实现这种操作吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-10-11 14:11:09

通常在 MongoDB 中,您会将答案嵌入到问题中。 99% 的情况下您都会通过问题进行查询,因此您不妨同时获得答案。

一些要求是我需要能够显示答案的数量...

如果您带回问题的答案,这真的很容易。您将拥有一个包含答案的数组/列表/集合。所以你只需抓住长度即可。

但是如何在不检索整个帖子的情况下插入答案

MongoDB 支持原子“$push”操作。这意味着您可以将项目添加到数组中,而无需实际从客户端加载文档。从 javascript shell 来看,它看起来像这样:

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

所以 MongoDB 能够做到这一点。您必须检查 NoRM 驱动程序,以确保它们实际上允许这种类型的行为(如果它们不支持 $push,它们确实会丢失一些东西)。

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).

薆情海 2024-10-11 14:11:09

答案应该是问题的一部分。

公开课问题
{
公共字符串 ID { 获取;放; 。

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

由于缺乏联接,文档数据库鼓励您将整个图的实例存储在一个文档中

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文