Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb updateitem only with global secondary index

Can we update a dynamodb item only with global secondary index?

$response = $dynamodbClient->updateItem(array(
            'TableName' => 'feed',
            'Key' => array(
                'feed_guid'      => array('S' => 'ac1e9683832ad2923f0bd84b91f34381'),
                'created_date'   => array('N' => '1439295833'),
            ),
            "ExpressionAttributeValues" =>  array (
                    ":val1" => array('N' => '1')
                ) ,
            "UpdateExpression" => $updateExpression,  
            'ReturnValues' => 'ALL_NEW'
        ));

In the above code, I want to replace the key section and update the item using a global secondary index, that is user_id.

like image 519
Arun SS Avatar asked Aug 11 '15 13:08

Arun SS


People also ask

Can DynamoDB update global secondary index?

No, you cannot update items in a GSI. You make changes/updates to items in the table and those updates are propagated to the GSIs. Save this answer.

What is the use of global secondary index in DynamoDB?

A global secondary index contains a selection of attributes from the base table, but they are organized by a primary key that is different from that of the table. The index key does not need to have any of the key attributes from the table. It doesn't even need to have the same key schema as a table.

Can a global secondary index create at the same time as the table creation?

AWS DynamoDB CLIYou can create a Global Secondary Index at the same time you create a DDB table using create-table command. You can also use AWS DynamoDB update-table command to create a Global Secondary Index after creating a DDB table.

Why would you use a secondary index on a DynamoDB table?

A secondary index is a data structure that contains a subset of attributes from a table, along with an alternate key to support Query operations. You can retrieve data from the index using a Query , in much the same way as you use Query with a table.


2 Answers

No, you cannot update items in a GSI. You make changes/updates to items in the table and those updates are propagated to the GSIs.

like image 86
mkobit Avatar answered Oct 18 '22 14:10

mkobit


A workaround I used for this was to first execute a query command using the document client, then extract the partition key from the result, and call the update method with my retrieved value as the key.

like image 36
Kyle McCurley Avatar answered Oct 18 '22 14:10

Kyle McCurley