Disclaimer - I'm not familiar with DynamoDb
, but have used several other NoSql databases
The common approach is to choose the most important subject entity as the root
of the document (in your case, I would say this is Person
)
A document is then created for each person, and will include the "person centric" view of all associated entities (i.e. linked sports):
Joe (Person, Keyed on a natural, or surrogate id).
+ Fields of Joe (Date of Birth, etc)
+ SportsPlayed: (Collection)
--> Golf (Sport)
--> Tennis (Sport)
If it becomes important to view the relationship from a Sport centric approach (e.g. you need to know which persons are 'subscribed' to which Sport):
You could attempt a secondary index on Person.Sport
, if the NoSql database allows this. This would allow for queries like "Who plays Golf?", although this approach is often frowned upon in NoSql terms.
Alternatively, and preferably, create a second collection of documents, this time keyed by Sport
:
Golf (Sport)
- Joe
- Jim
...
etc. Obviously there's extra work to be done in keeping both sets of documents up to date when a change is made to a Person
, a Sport
, or the relationship between them, however the benefit is high performance on the read side - only a single document needs to be retrieved to pull the entire entity graph - In SQL terms, this would have required a Query joining 3 distinct tables.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…