如何基于RavenDB中的索引更新文档?

如何基于RavenDB中的索引更新文档?,ravendb,Ravendb,以Stack Overflow标记系统为例,如何获取带有计数的标记对象 鉴于这些实体: // Represents the data from the TagCount index public class TagCount { public string Tag { get; set; } public int Count { get; set; } } // Represents the data used to populate a tag-wiki public cla

以Stack Overflow标记系统为例,如何获取带有计数的标记对象

鉴于这些实体:

// Represents the data from the TagCount index
public class TagCount
{
    public string Tag { get; set; }
    public int Count { get; set; }
}

// Represents the data used to populate a tag-wiki
public class Tag
{
    public string Tag { get; set; }
    public DateTime Created  { get; set; }
    public string Description { get; set; }
}

public class Question
{
    // elided
    public List<string> Tags { get; set; }
    // elided
}
在标记页面(例如),我们需要显示标记集合和标记计数索引的组合。也就是说,我们需要显示Tags集合中的Tag.Name和Tag.Description,以及TagCount索引中的TagCount.Count。在SQLServer中,这可以通过连接来实现,但这显然是关系思维方式。实现这一点的惯用方法是什么


有没有一种方法可以将Count属性添加到标记实体中,并让索引自动更新该属性?

您可以通过修补(在API中也称为UpdateByIndex)来实现这一点。 你可以在这里看到:

// Map
from question in docs.Questions
from Tag in question.Tags
select new { Tag, Count = 1 }

// Reduce
from result in results
group result by result.Tag into g
select new 
{
    Tag = g.Key,
    Count = g.Sum(x=>x.Count)
}