.net – RavenDb仅返回索引字段
我们假设我有以下索引:
public class PostsForList: AbstractIndexCreationTask<Post,PostsForList.ReduceResult> { public class ReduceResult { public string Id { get; set; } public string Title { get; set; } public long CommentsCount { get; set; } } public PostsForList() { Map = posts => from post in posts select new { Id = post.Id,Title = post.Title,CommentsCount = post.Comments,}; } } 如果我执行它,RavenDb将不仅返回Id,Title和CommentsCount,而且返回整个Post文档.但我不需要整个文档(让我们想象它包含很多其他东西). 正如我所说,现在有两种解决方案: >将索引字段标记为已存储并调用 那么为什么没有更自然的解决方案呢(或者我可能只是不知道它)?这是一个概念解决方案吗? 解决方法
您可以使用
Projections feature的RavenDB进行转换.
这只是在RavenDB将其从doc存储中拉出后改变了Json doc的形状,因此字段不必标记为Stored for it to working public PostsForList() { Map = posts => from post in posts select new { Id = post.Id,}; TransformResults = (database,posts) => from post in posts select new { post.Id,post.Title,post.CommentsCount }; } 你还需要我们.As< new type>当你查询这个索引时,否则RavenDB会抱怨.像这样的东西: session.Query<Post,PostTitleCommentsCount>() .Where(x => x.Title == "blah") .As<PostTitleCommentsCount>() .ToList(); 它最常用于允许连接,有关详细信息,请参阅this blog post ,但也适用于此方案. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |