RavenDb Map / Reduce嵌套数组

时间:2016-03-28 10:21:05

标签: ravendb

我试图在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
         }
             ]
}

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

索引需要在region和summoner_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)
}

1 个答案:

答案 0 :(得分:0)

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

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

但使用:

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

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

"Raven-Entity-Name": "Games"