RavenDB map / reduce索引为两级深层嵌套关系文档

时间:2016-02-10 00:23:23

标签: indexing ravendb

我有以下结构的工作文件。这些工作文件可能有相同的工作人员。我正在尝试创建一个map / reduce索引来计算作业列表的唯一机组成员总数,并且我不断得到总计数而不是总的唯一计数。

这是我尝试过的map / reduce索引,它返回了总计数而不是唯一计数。

    Map = jobs => from job in jobs
                  from crew in job.Crews
                  from member in crew.Members
                  select new { UserID = member.UserName, Count = 1 }

    Reduce = results => from result in results.Distinct()
                        group result by 0 into g
                        select new { UserID = "", g.Sum(x => x.Count) }


{
    "Name": "Planetary Laser",
    "Crews": [
        {
            "Name": "Laser Team 1",
            "Members": [
                {
                    "UserName": "vader@empire.com",
                },
                {
                    "UserName": "uno@empire.com",
                },
                {
                    "UserName": "dos@empire.com",
                },
            ],
        },
        {
            "Name": "Laser Team 2",
            "Members": [
                {
                    "UserName": "emporer@empire.com",
                },
                {
                    "UserName": "bfett@empire.com",
                },
                {
                    "UserName": "dos@empire.com",
                }
            ],
        }
    ],
}

1 个答案:

答案 0 :(得分:1)

你需要这样做:

Map = jobs => from job in jobs
                  from crew in job.Crews
                  from member in crew.Members
                  select new { UserID = member.UserName, Count = 1 }
Reduce = results => from result in results
                        group result result by result.UserId into g
                        select new { UserID = g.Key, g.Sum(x => x.Count) }

此索引为您提供每个用户的总作业数。并且索引中的用户总数是不同用户的数量。