RavenDB - 检索文档的一部分

发布于 2024-12-10 18:19:38 字数 166 浏览 0 评论 0 原文

我使用 Raven DB 几天了,我想用它作为我的网络聊天应用程序的存储。我有包含一些用户数据和聊天历史记录的文档 - 这是聊天消息的大集合。

每次我加载用户文档时,聊天历史记录也会加载,即使我只需要几个字段,例如:用户名、密码和电子邮件。

我的问题是:如何从数据库仅加载文档的一部分?

I am playing with Raven DB for few days and I would like to use it as a storage for my Web chat application. I have document which contains some user data and chat history - which is big collection chat messages.

Each time I load user document chat history is also loaded, even if I need only few fields like: user name, password and email.

My question is: how to load only part of document from database ?

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

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

发布评论

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

评论(3

盛夏尉蓝 2024-12-17 18:19:38

Tomek,

您无法加载部分文档,但您可以加载投影。

session.Query<User>()
   .Where(x=>x.Name == name)
   .Select( x=> new { x.Name, x.Email });

这将只加载适当的字段

Tomek,

You can't load a partial document, but you can load a projection.

session.Query<User>()
   .Where(x=>x.Name == name)
   .Select( x=> new { x.Name, x.Email });

That will load only the appropriate fields

像极了他 2024-12-17 18:19:38

从我所看到的,你可以这样做(基于上面的原始“用户”场景):

public class UserSummary
{
    public string Name { get; set; }
    public string Email { get; set; }
}

然后你可以这样做:

documentSession.Query<User>().AsProjection<UserSummary>();

查看Raven服务器,它将此作为查询的一部分吐出:

?query=&pageSize=128&fetch=Name&fetch=Email&fetch=Id

所以它看起来像是在查询并仅返回原始对象的子集,这很好。

这也有效:

documentSession.Query<User>().Select( x=> new User { Name = x.Name, Email= x.Email })

但我认为这不如返回 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):

public class UserSummary
{
    public string Name { get; set; }
    public string Email { get; set; }
}

Then you can do this:

documentSession.Query<User>().AsProjection<UserSummary>();

Looking at the Raven server it spits this out as part of the query:

?query=&pageSize=128&fetch=Name&fetch=Email&fetch=Id

So it looks like it is querying and returning only a subset of the original object, which is good.

This also works:

documentSession.Query<User>().Select( x=> new User { Name = x.Name, Email= x.Email })

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?

我一向站在原地 2024-12-17 18:19:38

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.

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