【问题标题】:RavenDB: How to create MapReduce index to return a filtered list of child objectsRavenDB:如何创建 MapReduce 索引以返回过滤的子对象列表
【发布时间】:2012-02-28 02:23:07
【问题描述】:

关注this question

我的文档结构如下:

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)
                                };


        }
    }

【问题讨论】:

    标签: ravendb indexing


    【解决方案1】:

    Marto,如果您真的想要以这种方式建模,则无需创建索引,因为您可以加载整个游戏文档并使用标准 linq-to 完成其余工作-对象聚合。

    无论如何,听起来好像您希望将 Game、Player 和 Event 作为独立的文档,因为它们是您的聚合根,或者换句话说 - 它们本身就有意义。

    【讨论】:

    • 公平点。然而,玩家不是完整的实体,而只是一个名称和一个 ID,所以我认为他们不应该生活在 Game 对象之外。我最初考虑将事件拆分为一个 GameEvents 对象,因为它们在整个游戏中最常更新,但我仍然会遇到类似的问题。你认为我真的需要分离模型才能查询吗?
    • 不,您根本不需要索引来查询它,因此在单独的文档中对其进行建模只是一个建议。正如我上面所说,您只需加载文档并执行正常的 linq 查询即可。
    猜你喜欢
    • 2012-01-17
    • 1970-01-01
    • 2020-05-09
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多