地图或减少谓词(RavenDb)?

时间:2012-03-22 13:00:13

标签: ravendb

如果我想在我在Reduce函数中聚合之前将谓词应用于文档,我是否要将该谓词放在Map函数或Reduce函数中?

因此,例如将谓词放在Map函数中将如下所示:

        Map = orders => orders
            .Where(order => order.Status != OrderStatus.Cancelled)
            .Select(order => new
                {
                    Name = order.Firstname + ' ' + order.Lastname,
                    TotalSpent = order.Total,
                    NumberOfOrders = 1
                });
        Reduce = results => results
            .GroupBy(result => result.Email)
            .Select(customer => new
                {
                    Name = customer.Select(c => c.Name).FirstOrDefault(),
                    TotalSpent = customer.Sum(c => c.TotalSpent),
                    NumberOfOrders = customer.Sum(c => c.NumberOfOrders)
                });

将它放在Reduce函数中将如下所示:

        Map = orders => orders
            .Select(order => new
                {
                    Name = order.Firstname + ' ' + order.Lastname,
                    TotalSpent = order.Total,
                    NumberOfOrders = 1,
                    Status = order.Status
                });
        Reduce = results => results
            .Where(order => order.Status != OrderStatus.Cancelled)
            .GroupBy(result => result.Email)
            .Select(customer => new
                {
                    Name = customer.Select(c => c.Name).FirstOrDefault(),
                    TotalSpent = customer.Sum(c => c.TotalSpent),
                    NumberOfOrders = customer.Sum(c => c.NumberOfOrders),
                    Status = (OrderStatus)0
                });

后者显然更有意义,但这意味着我必须将Status属性添加到R​​educe结果的类中,然后在Reduce中将其设置为某个未知值,因为它实际上并不代表任何东西。

1 个答案:

答案 0 :(得分:4)

只有第一种方法适用于map / reduce。不,订单将被忽略,您不能在结果中执行类似FirstOrDefault的操作。

您需要将map / reduce视为两个独立函数,而reduce函数可以在同一输入上运行多次,这就是输入格式必须与输出格式匹配的原因。这也可以并行和异步地在不同的服务器上进行,因此在索引运行时可以保存新文档。