RavenDB Map / Reduce按日期分组

时间:2012-08-10 11:12:26

标签: mapreduce ravendb

我必须创建一个查询,以便按照每年/每月的帖子获取统计信息,例如按日期分组。我创建了一个索引:

public class Posts_Count : AbstractIndexCreationTask<Post, ArchiveItem>
{
    public Posts_Count()
    {
        Map = posts => from post in posts
              select new
                         {
                             Year = post.PublishedOn.Year,
                             Month = post.PublishedOn.Month,
                             Count = 1
                         };

        Reduce = results => from result in results
                            group result by new {
                                 result.Year,
                                 result.Month
                            }
                            into agg
                            select new
                                       {                                               
                                           Year = agg.Key.Year,
                                           Month = agg.Key.Month,
                                           Count = agg.Sum(x => x.Count)
                                       };
    }
}

在工作室中,我有下一个地图和缩小功能:

地图:

docs.Posts.Select(post => new {Year = post.PublishedOn.Year, Month = post.PublishedOn.Month, Count = 1})

减少

results
.GroupBy(result => new {Year = result.Year, Month = result.Month})
.Select(agg => new {Year = agg.Key.Year, Month = agg.Key.Month, Count = agg.Sum(x => ((System.Int32)(x.Count)))})

但问题是我总是得到年和月属性的空值:

{
   "Year": null,
   "Month": null,
   "Count": "1"
}

有人可以帮我解决我的代码问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好。我测试了它,它适用于当前不稳定的版本1.2.2096。最近在RavenDB google小组上进行了一些讨论,所以可能之前已经破了。请使用当前版本重试,看看它现在是否适合您。