1

I have a DDB table with hash key as id (string) and sort key as flag (boolean). I want to get all the items in the table which has flag value of true. I have not setup any GSI or LSI but I can create them if required.

Schema

{
  "id": {
    "S": "<Some ID>"
  },
  "flag": {
    "B": "<true/false>"
  }
}

2 Answers2

4

You say that you have a boolean sort key, however a DynamodDB sort key of type Boolean is not supported. Instead you could use a number (0/1) or string ("false"/"true") to represent a boolean. You could also consider making the index sparse if it is not the sort key.

To query all items with true (false) for a sort key, you will need a GSI since an LSI is limited in scope to a single hash key. So you should probably create a GSI on the "boolean" (actually string or number) field. Note that GSIs can only be queried with eventual consistency, so consider your use case before selecting a GSI.

Ben
  • 1,759
  • 1
  • 19
  • 23
0

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

swayamraina
  • 2,958
  • 26
  • 28