【问题标题】:How to do a MaxBy in RavenDb MapReduce如何在 RavenDb MapReduce 中执行 MaxBy
【发布时间】:2017-08-19 18:07:52
【问题描述】:

使用 RavenDB 教程中的 Northwind 数据库,我正在尝试按员工对订单进行分组,并为每位员工获取最近发送的订单。

地图:

from order in docs.Orders
select new {
    Employee = order.Employee,
    Count = 1,
    MostRecent = order.OrderedAt,
    MostRecentOrderId = order.Id
}

使用不存在的 MaxBy 减少:

from result in results
group result by result.Employee into grp
select new {
    Employee = grp.Key,
    Count = grp.Sum(result => result.Count),
    MostRecent = grp.Max(result => result.MostRecent),
    MostRecentOrderId = grp.MaxBy(result => result.MostRecent).MostRecentOrderId,
}

减少尝试:

from result in results
group result by result.Employee into grp
let TempMostRecent = grp.Max(result => result.MostRecent)
select new {
    Employee = grp.Key,
    Count = grp.Sum(result => result.Count),
    MostRecent = TempMostRecent,
    MostRecentOrderId = grp.First(result => result.MostRecent == TempMostRecent).MostRecentOrderId
}

但是我的 reduce 尝试返回 0 个结果。

另外:RavenDB 会将Order.OrderetAt 视为正确的DateTime 值并正确排序吗?

【问题讨论】:

    标签: mapreduce ravendb


    【解决方案1】:

    你需要这样做

    from order in docs.Orders
    select new {
        Employee = order.Employee,
        Count = 1,
        MostRecent = order.OrderedAt,
        MostRecentOrderId = order.Id
    }
    
    from result in results
    group result by result.Employee into grp
    let maxOrder = grp.OrderByDescending(x=>x.MostRecent).First()
    select new {
        Employee = grp.Key,
        Count = grp.Sum(result => result.Count),
        MostRecent = maxOrder.MostRecent,
        MostRecentOrderId = maxOrder.MostRecentOrderId,
    }
    

    【讨论】:

    • 1.谢谢 - 它有效。 2. 现在我看到我的 Reduce 产生了错误,但是我不明白原因。 TempMostRecentIComperable,我觉得应该是System.DateTime,为什么是IComperable
    • 在服务器端,日期被保存为字符串,也许这有关系?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多