如何在RavenDB多地图索引中执行嵌套组

时间:2013-01-28 22:24:53

标签: c# linq mapreduce ravendb

我的RavenDB数据库中有两个不同的文档集 - 团队和匹配。文件如下:

public class Team {
    public string Id { get; set; }
    public string Name { get; set; }
    public int LeaguePosition { get; set; }
}

public class Match {
    public string Id { get; set; }
    public string HomeTeamName { get; set; }
    public string AwayTeamName { get; set; }
    public DateTime StartTime { get; set; }
}

所以基本上我在这些团队之间有团队和比赛。但是,对于某些操作,我需要从数据库中获得一个类似于以下内容的实体:

public class MatchWithExtraData {
    public string Id { get; set; } // Id from the match document.
    public string HomeTeamId { get; set; }
    public string HomeTeamName { get; set; }
    public int HomeTeamPosition { get; set; }
    public string AwayTeamId { get; set; }
    public string AwayTeamName { get; set; }
    public int AwayTeamPosition { get; set; }
    public DateTime? StartTime { get; set; }
}

我想要的确实是比赛文件,但主场和客场球队的比赛和联赛位置都有额外的领域。基本上将主队和客队名称的比赛文件加入两个团队文件,一个用于主队,一个用于客队。我认为多map / reduce索引应该可以解决这个问题所以我已经开始使用以下索引:

public class MatchWithExtraDataIndex: AbstractMultiMapIndexCreationTask<MatchWithExtraData> {
    public MatchWithExtraData() {
        AddMap<Team>(
            teams => from team in teams
                     select new {
                         Id = (string)null,
                         HomeTeamId = team.Id,
                         HomeTeamName = team.Name,
                         HomeTeamPosition = team.LeaguePosition,
                         AwayTeamId = team.Id,
                         AwayTeamName = team.Name,
                         AwayTeamPosition = team.LeaguePosition,
                         StartTime = (DateTime?)null
                     }
        );
        AddMap<Match>(
            matches => from match in matches
                       select new {
                           Id = match.Id,
                           HomeTeamId = (string)null,
                           HomeTeamName = match.HomeTeamName,
                           HomeTeamPosition = 0,
                           AwayTeamId = (string)null,
                           AwayTeamName = match.AwayTeamName,
                           AwayTeamPosition = 0,
                           StartTime = match.StartTime
                       }
        );
        Reduce = results => from result in results
                            // NOW WHAT?
    }
}

由于每场比赛中有两支球队,因此减少部分是我无法弄清楚的部分。我想我需要做一个嵌套组,首先在HomeTeamName上,然后在AwayTeamName上,但我无法弄清楚如何做到这一点。

这可能是一个LINQ问题而不是RavenDB问题。但是这样一个嵌套的群体怎么会看起来呢?或者可以用另一种方式完成?

1 个答案:

答案 0 :(得分:0)

您最好使用转换结果或包括。 请参阅此处的文档:http://ravendb.net/docs/client-api/querying/handling-document-relationships