0

I have a table in dynamoDb with a primary hashKey itemId. Items also have a relationship to a parent item, so besides querying them by its itemId, I also want to be able to retrieve all the children of a parent, so I added another attribute parentId.

Now I want to be able to find all the children for a given parent. Reading the docs, I understand that I need to create a secondary index, but I fail to understand what my rangeKey should be and how to construct a DynamoDBQueryExpression<Item> without setting the range condition.

I am missing something fundamental I guess...

user331244
  • 601
  • 9
  • 19

1 Answers1

1

I'm assuming that your table looks something like this:

itemId (hash key), parentId

You need to create a Global Secondary Index on this table that looks something like this:

parentId (hash key), itemId (range key)

Now, you can do a Query where you set the parent ID in the KeyConditionExpression.

Here's an example written in Ruby:

resp = Aws::DynamoDB::Client.new.query(
  table_name: "MyTableName",
  select: "SPECIFIC_ATTRIBUTES",
  attributes_to_get: ["parentId", "itemId"],
  key_condition_expression: "parentId = <parent_whose_children_you_want>",
  exclusive_start_key: <query_start_key_from_prev_query>
)

Once DynamoDB hits 1MB of data, it'll return and give you a LastEvaluatedKey that you can use as the ExclusiveStartKey for your next query. This way you can page through your DB.

readyornot
  • 2,783
  • 2
  • 19
  • 31