【问题标题】:RavenDb Map/Reduce Nested ArraysRavenDb 映射/减少嵌套数组
【发布时间】:2016-03-29 17:31:38
【问题描述】:

我正在尝试在 RavenDb 中创建等效的 group_by/mysql

Json 文档长这样:

{
 "region" : "EUW",
 "players" : [
         {
            "position" : 2,
            "summoner_id" : 123456,
            "game_won": 1
         },
         {
            "position" : 1,
            "summoner_id" : 123459,
            "game_won": 0
         },
         {
            "position" : 3,
            "summoner_id" : 123458,
            "game_won": 1
         },
         {
            "position" : 4,
            "summoner_id" : 123457,
            "game_won": 0
         }
             ]
}

有多个这样的文件,我需要找出 summener_id 123456 有多少次获得位置 2 或任何其他位置 1-6 以及他在该位置赢得了多少次

索引需要在区域和召唤者ID上是可查询的

结果看起来像

 {
   "positions" : 
         [
           { "position" : 1,
             "total" : 123,
             "won" : 65
           },
           { "position" : 2,
             "total" : 37,
             "won" : 10
           }
         ]
   }

我目前拥有的

地图

from game in docs.Games
from player in game.Players
select new {
    player.position,
    player.summoner_id,
    game.region,
    PosCounter = 1,
    WonCounter = player.game_won == 1 ? 1 : 0
}

减少

from result in results
group result by result.Position into g
select new {
    g.Key.position,
    g.Key.summoner_id,
    g.Key.region,
    PosCounter = g.Sum(x => x.PosCounter),
    WonCounter = g.Sum(x => x.WonCounter)
}

【问题讨论】:

    标签: ravendb


    【解决方案1】:

    属性名称区分大小写。你需要:

    {
     "region" : "EUW",
     "players" : [
             {
    

    但使用:

    from game in docs.Games
    from player in game.Players
    

    此外,在文档的元数据中,请确保您有:

    "Raven-Entity-Name": "Games"
    

    【讨论】:

      猜你喜欢
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2012-03-09
      相关资源
      最近更新 更多