RavenDB - 检索文档的一部分
我使用 Raven DB 几天了,我想用它作为我的网络聊天应用程序的存储。我有包含一些用户数据和聊天历史记录的文档 - 这是聊天消息的大集合。
每次我加载用户文档时,聊天历史记录也会加载,即使我只需要几个字段,例如:用户名、密码和电子邮件。
我的问题是:如何从数据库仅加载文档的一部分?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Tomek,
您无法加载部分文档,但您可以加载投影。
这将只加载适当的字段
Tomek,
You can't load a partial document, but you can load a projection.
That will load only the appropriate fields
从我所看到的,你可以这样做(基于上面的原始“用户”场景):
然后你可以这样做:
查看Raven服务器,它将此作为查询的一部分吐出:
所以它看起来像是在查询并仅返回原始对象的子集,这很好。
这也有效:
但我认为这不如返回 UserSummary 对象那么干净。
对那些发布回复的人提出一些后续问题:
RaccoonBlog 的链接有以下示例:
https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Infrastruct/Indexes/PostComments_CreationDate.cs
该方法会优于 .AsProjection() 吗?这两种方法有什么区别?
From what I've seen you can do this (based on the original "User" scenario above):
Then you can do this:
Looking at the Raven server it spits this out as part of the query:
So it looks like it is querying and returning only a subset of the original object, which is good.
This also works:
But I don't think that is as clean as returning a UserSummary object.
Some follow up questions to those who have posted responses:
The link to RaccoonBlog has this example:
https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Infrastructure/Indexes/PostComments_CreationDate.cs
Would that method be preferred over the .AsProjection()? What is the difference between the two approaches?
Tomek,您不能只加载文档的一部分。
不过,我理解你的情况的问题。我建议为每个用户使用两个单独的文档:一个实际包含用户数据(姓名、密码哈希、电子邮件等),另一个包含所有用户消息。这样,加载用户的所有消息以及加载通用用户列表仍然非常便宜。
这实际上与博客域的建模方式非常相似,其中您有一篇帖子和帖子评论。查看 RaccoonBlog 了解其工作原理。
Tomek, you cannot load only a part of the document.
However, I understand the problem in your case. I recommend to use two seperate documents for each user: One that actually contains the users data (name, passwordhash, email, etc.) and one that contains all the users messages. That way, it is still very cheap to load all the messages of a user and also to load a list of user for general purposes.
This is actually quite similar to how one would model a blog-domain, where you have a post and the posts comments. Take a look at RaccoonBlog to see how this works.