DynamoDB is a distributed data-store i.e. it stores the data not in a single server but does partitions using the provided partition key (PK).
This means your data is spread across multiple servers and brings the limitation that you can query a single partition at a time.
Since you have a hash key
as PK that means your flag
is spread across partitions and a normal query with PK, SK will never give you the complete data.
To query on a global level i.e. all partitions you need to create a GSI.
for the query get all items with flag as true
:
- Add a new attribute to the schema for items when flag is marked = true
- Create a GSI with PK as hash and SK as this new attribute and data you need to get to the attributes
When you update an item and add the new attribute, it will create an entry in GSI. as well and hence your GSI will only have entries where flag = true