Dynamodb updateitem only with global secondary index
Asked Answered
T

2

11

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.

Titre answered 11/8, 2015 at 13:30 Comment(0)
F
13

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.

Friulian answered 11/8, 2015 at 15:27 Comment(0)
E
3

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.

Extinction answered 15/4, 2021 at 22:1 Comment(3)
Avoid using this solution if you worry about concurrencyOversold
@Oversold Is there a better option?Dupont
@Daniel yes, with versioning control of the row (act as a lock). Update a row by applying a unique version, process your stuff backend, then when writing back to the item use a transaction to apply a conditional check for the unique having to be unchangedOversold

© 2022 - 2024 — McMap. All rights reserved.