2012-02-27 51 views
1

this questionRavenDB:如何创建MapReduce的索引返回儿童的筛选对象列表

我有以下的文档结构:

Game 
- Id 
- Teams 
    - Team 1 
    - list of players 
    - Team 2 
    - list of players 
- Events 
    - Event 1 
    - type: Pass 
    - teamId 
    - PlayerId 
    - Event 2 
    - type: Goal 
    - teamId 
    - PlayerId 

如何建立,让我所有的事件的索引对于给定游戏的玩家?

这是我得到多远,RavenDB说它不能理解我的查询?

public class Games_PlayerEvents : AbstractIndexCreationTask<Game, Games_PlayerEvents.ReduceResult> 
    { 
     public class ReduceResult 
     { 
      public string PlayerId { get; set; } 
      public IEnumerable<GameEvent> Events { get; set; } 
     } 


     public Games_PlayerEvents() 
     { 
      Map = games => from game in games 
          select new 
          { 
           PlayerId = "", 
           Events = game.Events 
          }; 

      Reduce = results => from result in results 
           from @event in result.Events 
           group @event by @event.PlayerId into playerEvents 
           select new 
           { 
            PlayerId = playerEvents.Key, 
            Events = playerEvents.Select(g => g) 
           }; 


     } 
    } 

回答

3

Marto,如果你真的给它这样的模型,你不需要创建一个索引,你可以加载整个文档的游戏,并使用标准的LINQ的待办事项休息对象聚合。

无论如何,它听起来好像你想让游戏,玩家和事件作为独立的文档,因为这些是你的聚合根或换句话说 - 它们有自己的意义。

+0

公平点。然而,玩家不是完整的实体,而只是一个名字和一个ID,所以我不认为他们应该生活在Game对象之外。我原本以为将事件分成GameEvents对象是因为它们在整个游戏中经常更新,但我仍然会遇到类似的问题。你认为我真的需要分开模型来查询它吗? – marto 2012-02-27 22:33:48

+1

不,你不需要索引就可以查询它,所以在单独的文档中建模只是一个建议。正如我上面所说的,只需加载文档并执行正常的linq查询即可。 – 2012-02-27 23:55:38