使用NoRM在MongoDB中进行延迟加载 [英] Lazy loading in MongoDB with NoRM

查看:111
本文介绍了使用NoRM在MongoDB中进行延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此相似的模型: (简体)

I have a model similar to this: (simplified)

问题:

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

    public string Title { get; set; }
    public string Body { get; set; }

    public List<Answer> Answers { get; set; }
}

答案:

public class Answer
{
    public string QuestionID { get; set; }
    public string Body { get; set; }
}

我打算将数据存储在MongoDB中,并希望与此配合使用 NoRM .

I intend to store the data in MongoDB, and would like to use NoRM with this.

我的问题是:是否支持延迟加载?还是可以将其设置为在文档存储上进行延迟加载??

My question is: Is lazy loading supported? Or can I set it up to do lazy-loading on the document store..?

这样请求一个问题,还会检索答案..? (这两个都是帖子",存储在MongoDB的同一集合中)

So that requesting a Question, also retrieves the Answers..? (both will be "posts" stored in the same collection on MongoDB)

推荐答案

好的,延迟加载"的概念对于MongoDB这样的数据库来说是陌生的.看一下您的架构:QuestionListAnswers.

OK, the concept of "Lazy Loading" is mostly foreign to a database like MongoDB. Take a look at your schema: Question has a List of Answers.

RDBMS 中,惰性"部分允许您与原始文件分开加载列表".实际上发生了两个查询,您只是想延迟第二个查询.

In an RDBMS the "lazy" part allows you to load "the list" separately from the original. There are actually two queries happening, you're just trying to delay the second query.

MongoDB 中,仅发生一个查询. Answers嵌入在问题中,因此您对Questions的请求会自动包含Answers的列表.

In MongoDB there's only one query happening. The Answers are embedded inside of the question, so your request for Questions automatically includes the list of Answers.

请查看NORM示例,以获得更好的示例: http://normproject.org/samples

Please take a look at the NORM samples for a better example of this: http://normproject.org/samples

基本要点是,您提供的结构不再是多个表.这只是带有嵌入式文档的一个集合.因此,延迟加载"的概念实际上是不必要的,因为您不能对一个查询进行延迟加载".

The basic point is that the structure you provided is no longer multiple tables. It's just one collection with embedded documents. So the concept of "Lazy Loading" is really unnecessary because you can't "Lazy Load" one query.

这篇关于使用NoRM在MongoDB中进行延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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