Let's say I have a DynamoDB table modelling two entities: people and the cities they have visited, in a many-to-many relationship.
personName
could be the primary key, and cityName
could be the sort key. We could also invert the index to run get operations by cityName
.
What I would like to do, is for a given city, find the most related cities. For example, if London
is the city, what are the cities that have the most visitors in common with London
?
One way to do this, with two DB queries, is to get all visitors to London, then get all the cities those visitors have been to, count, and rank them.
Query one: getPeople("London") --> ["Paul", "Anna", "Mark", "Susan"]
Query two: getCities(["Paul", "Anna", "Mark", "Susan"]) --> ["Berlin", "Oslo", "Oslo", "London", ...]
Result: relatedTo("London") --> {"London": 4, "Oslo": 2, ...
However, is there some way to model this in DynamoDB, so that it can be achieved in a single operation?