使用 NoRM 在 MongoDB 中进行延迟加载
我有一个与此类似的模型: (简化)
问题:
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 与此。
我的问题是:支持延迟加载吗?或者我可以将其设置为在文档存储上进行延迟加载..?
那么请求一个问题,也会检索到答案..? (两者都将是存储在 MongoDB 上同一集合中的“帖子”)
I have a model similar to this:
(simplified)
Question:
public class Question
{
public string QuestionID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public List<Answer> Answers { get; set; }
}
Answer:
public class Answer
{
public string QuestionID { get; set; }
public string Body { get; set; }
}
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..?
So that requesting a Question, also retrieves the Answers..?
(both will be "posts" stored in the same collection on MongoDB)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,“延迟加载”的概念对于 MongoDB 这样的数据库来说大多是陌生的。看一下您的架构:
问题
有一个答案
的列表
。在RDBMS中,“惰性”部分允许您从原始列表中单独加载“列表”。实际上发生了两个查询,您只是想延迟第二个查询。
在 MongoDB 中,只发生一次查询。
答案
嵌入在问题中,因此您对问题
的请求会自动包含答案
列表。请查看 NORM 示例以获得更好的示例:http://normproject.org/samples
基本点是您提供的结构不再是多个表。它只是一个带有嵌入式文档的集合。因此,“延迟加载”的概念确实没有必要,因为您无法“延迟加载”一个查询。
OK, the concept of "Lazy Loading" is mostly foreign to a database like MongoDB. Take a look at your schema:
Question
has aList
ofAnswers
.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.
In MongoDB there's only one query happening. The
Answers
are embedded inside of the question, so your request forQuestions
automatically includes the list ofAnswers
.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.
我知道这是一个旧线程,但其他人可能仍在找到它(就像我一样)。延迟加载在 MongoDB 中是可能的,并且受 C# 驱动程序支持。
查看以下类:LazyBsonDocument 和 LazyBsonArray
来自 C# 驱动程序教程文档:“惰性类的特殊之处在于,它们将 BSON 的反序列化推迟到需要时为止。当您只需要一个或两个字段时,这非常有用。复杂的文档,因为它不会产生反序列化整个文档或数组的成本,而只会产生反序列化所需的部分。”
在撰写本文时,可以在此处找到教程文档: http://docs .mongodb.org/ecosystem/tutorial/use-csharp-driver/
给出的示例与问题非常相似,因为它涉及嵌套集合,因此看起来答案列表确实可以延迟加载,如果这是可取的。
希望这对某人有帮助,
尼克
I appreciate that this is an old thread, but other people may still be finding it (as I did). Lazy loading is both possible in MongoDB and supported by the C# driver.
Check out the following classes: LazyBsonDocument and LazyBsonArray
From the C# Driver tutorial documentation : "The lazy classes are special in that they defer the deserialization of BSON until it is needed. This is useful for when you only need a field or two out of a complex document because it will not incur the cost of deserializing the entire document or array, but just the pieces that are necessary. This deserialization occurs a level at a time."
At the time of writing the tutorial document can be found here: http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/
The example given is very similar to the question in that it involved a nested collection, so it looks like the list of answers could indeed be lazy loaded if that was desirable.
Hope this helps someone,
Nick