Search code examples
amazon-dynamodbdynamodb-queries

How to update items based on just the partition key?


Our table has a composite primary key. There are a few use cases where we need to update the items based on just the partition key.

partitionKey    sortKey
------------    ---------
10020525        208025117-xxxxx-153068323-208025401
10020525        208025117-208025475-153068323-208025401

Following is how I am currently updating: (Using Java)

UpdateItemRequest updateItemRequest = new UpdateItemRequest().withTableName("table").withKey(attMap);
UpdateItemResult updateItemResult = amazonDynamoDB.updateItem(updateItemRequest);

The 'attMap' already has the 'partitionKey' and 'sortKey'. Therefore, it does take care of updating the record matches the partition and sort key.

In a few cases (where there is a 'xxxxx' in the sort key), I want to update based on just the partitionKey.

Let me know if something is not clear. Would be happy to explain further.


Solution

  • Updating multiple items at once is not supported by DynamoDB. For this reason you cannot make update requests with just the partition key unless your table schema only has a partition key and no sort key.

    This is probably not the answer you were hoping for but it is how DynamoDB works. You have to consider your use-cases carefully and choose a schema that works, or consider different database solutions.