Search code examples
amazon-web-servicesamazon-dynamodb

AWS Dynamodb GSI - can we create partition key if our key is present in array of object in OG table


I have a table called "Purchase_List" that includes a property named "bills". This "bills" property is an array of objects. Your objective is to query the elements inside the "bills" property, so you came up with an idea to create a Global Secondary Index (GSI). In this GSI, you plan to use the "id" present inside the "bills" array as the partition key. However, you're unsure whether this is a valid approach. Could you please clarify if this is possible?


Solution

  • It's not possible to create a Global Secondary Index on a nested value, be that in a list type or map type.

    Tables and index partition keys must be top level attributes with valid data types: String, Number, Binary.

    From your description, for each object in the bill type, I would create a new item, this is known as vertically sharding your item. That would allow you to have ID as the top level attributes allowing you to index as needed.