I'm new to DynamoDB and went through the developer guide [1] and created this movies database [2] and I'm able to query the database for specific years and titles easily, as they're part of the primary key.
But I would like to retrieve the following two things and I wonder why these examples seem to be covered nowhere:
The examples I found are talking about adding a GSI for a simple field (like rating or a string field), but I can't figure out how I would do this when my field is a list by itself (like genres).
[1] https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.JavaScript.html [2] https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip
This is an example data point from the developer guide's movie database:
{
"year" : 2013,
"title" : "Turn It Down, Or Else!",
"info" : {
"directors" : [
"Alice Smith",
"Bob Jones"
],
"release_date" : "2013-01-18T00:00:00Z",
"rating" : 6.2,
"genres" : [
"Comedy",
"Drama"
],
"image_url" : "http://ia.media-imdb.com/images/N/O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
"plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
"rank" : 11,
"running_time_secs" : 5215,
"actors" : [
"David Matthewman",
"Ann Thomas",
"Jonathan G. Neff"
]
}
}
You cannot achieve what you want with the given data structure alone because indexes can only be used with top level string, number and binary attributes: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
With DynamoDB you achieve these kind of many-to-many relations when you denormalize the data by duplication. You could e.g. have Partition key with movie “title” (assuming it’s unique in this example) and sort key “genre”. Then you duplicate the data as many times as the movie belongs to different genres.
In order to get all genres, you can create global secondary index to genre attribute. And with the same index you can query specific genre and all movie titles in it.
AWS docs explains more about many-to-many relations here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html
Also this blog post is good read to get your mind away from normalized relational data approach to different denormalization approaches with DynamoDB: https://www.alexdebrie.com/posts/dynamodb-one-to-many/#composite-primary-key--the-query-api-action