问题
I'm new to RavenDB and I'm still trying to get my head around the best way to model the data for the current scenario. Here is what the data looks like.
Game
- Teams
- Team 1
- list of players
- Team 2
- list of players
- Events
- Event 1
- type: Pass
- teamId
- PlayerId
- Event 2
- type: Goal
- teamId
- PlayerId
At the beginning of each game we get the overall info for the game (e.g. Teams, Venue etc) and then every few minutes we get an updated list of events. Also I need to be able to query data for a particular player thoughtout the game (e.g. How many passes a player has)
Do I store it as a single document? Do I split the Events into a separate document e.g. GameEvents? Is there a third scenario?
回答1:
Store it as a single object - the structure you've defined is great. Then just define indexes for the various types of queries you will be doing. Not having to break things down into tables with relationships is what makes document DBs like Raven awesome - great for scenarios just like what you are describing.
回答2:
I wouldn't worry about how this will be stored in RavenDB. That's the beauty of document databases; don't think relationally. Create your domain model in the object-oriented way that it should be created (a Team would have a List<Player> property, etc...), then just save the entities as necessary.
I've been meaning to blog about how I kept my domain model pure while using RavenDB. I need to publish that...
** EDIT ** I finally published that blog: http://bit.ly/xUsYJK. This shows how Presto kept a somewhat pure domain model while using RavenDB.
By the way, Daniel Lang has a good blog about this subject:
http://daniellang.net/how-to-handle-relations-in-ravendb/
I use the Include<T> approach because I like to keep my domain entities referencing each other in what I consider an appropriate way.
Daniel also has a section called "Denormalize your references." Some people prefer that method.
回答3:
Thinking about how many such events can occur during a game, I believe that you definitely want to have them as seperate documents. That way you don't need to load and update the game document on each event coming in, as this would also be quite expensive if the documents grows very large.
To get statistics across all events of a game, I'd rather have some indexes that collect the apppropriate data.
来源:https://stackoverflow.com/questions/9468253/how-to-model-football-game-statistics-in-ravendb