Mapreduce 按日期分组的RavenDB映射/缩减

Mapreduce 按日期分组的RavenDB映射/缩减,mapreduce,ravendb,Mapreduce,Ravendb,我必须创建一个查询,以获得每年/每月的邮寄统计数据,例如按日期分组。我创建了一个索引: public class Posts_Count : AbstractIndexCreationTask<Post, ArchiveItem> { public Posts_Count() { Map = posts => from post in posts select new

我必须创建一个查询,以获得每年/每月的邮寄统计数据,例如按日期分组。我创建了一个索引:

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)
                                       };
    }
}
减少:

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.2.2096中工作。最近,RavenDB谷歌集团对此进行了一些讨论,因此,也许它之前已经被打破了。请使用当前版本再试一次,看看它现在是否适合您。

hmm,上面的索引看起来不错,但我们是否需要为每个项目定义排序选项?例如
Sort(x=>x.Year,SortOptions.Int);排序(x=>x.Month,SortOptions.Int);排序(x=>x.Count,SortOptions.Int)?否则,我们不会得到排序不一致的结果吗?
{
   "Year": null,
   "Month": null,
   "Count": "1"
}