使用C#/ASP.net MVC为NoSQL论坛应用程序建模 [英] Modelling a NoSQL Forum Application with C# / ASP.net MVC

查看:109
本文介绍了使用C#/ASP.net MVC为NoSQL论坛应用程序建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发基于论坛(问题/答案)的应用程序.
使用C#ASP.net MVC和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; }
}

答案

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

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

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?

推荐答案

通常在MongoDB中,您会将答案嵌入问题中.您有99%的时间要按问题进行查询,因此您最好同时获得答案.

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支持原子的"$ push"操作.这意味着您可以将项目添加到数组中,而无需实际从客户端加载文档.从javascript外壳来看,看起来像这样:

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

因此MongoDB有能力做到这一点.您必须与NoRM驱动程序进行核对,以确保它们确实允许这种类型的行为(如果它们不支持$ push,它们确实会丢失某些东西).

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

这篇关于使用C#/ASP.net MVC为NoSQL论坛应用程序建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆