RavenDB Map-Reduce Example using .NET Client

前端 未结 2 737
闹比i
闹比i 2020-12-23 10:14

I\'m looking for an example of how to implement and use Map-Reduce within the RavenDB .NET Client.

I\'d like to apply it to a specific scenario: generating unique a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 11:01

    Here is how you can build an index for unique visitors:

    public class Statistics_UniqueVisitors : AbstractIndexCreationTask
    {
        public Statistics_UniqueVisitors()
        {
            Map = entries => from entry in entries
                             select new { entry.UserId, Count = 1 };
            Reduce = results => from result in results
                                group result by result.UserId into g
                                select new { UserId = g.Key, Count = g.Sum(x=>x.Count) };
        }
    }
    

    You can then query this using:

    var numberOfUniqueVisitors = s.Query().Count();
    

    For total count of visitors, you can use:

    var numberOfVisitors = s.Query().Count();
    

提交回复
热议问题