Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update list element in dynamodb

I have a list of maps as one field of a DynamoDB table. How can I update a specific element (or, rather element field ?) Trying something like

rc = table.update_item(Key={ 'username' : user }, 
    UpdateExpression="set list[:i].field = :nd",
    ExpressionAttributeValues={
        ':i' : itemnum,
        ':nd': data,
    },
    ReturnValues="UPDATED_NEW"
);

But I am getting an error:

Invalid UpdateExpression: Syntax error; token: ":i", near: "[:i]"

Any ideas how can I reference list element with variable number. Thanks.

like image 306
VitalyR Avatar asked Apr 22 '16 17:04

VitalyR


People also ask

How do you update multiple items in a DynamoDB table?

The only way to update multiple items at the same time is use TransactionWrite operation provided by DynamoDB. But it comes with a limitation (25 at most for example). So keep in mind with that, you probable should do some limitation in your application as well.

Can you update sort key value DynamoDB?

Can we update the sort key in DynamoDB? No, you can not update the sort key after the table is provisioned. However, you can create a new table and put the existing data in the newly created table, and delete the old table.

Does Put item update item DynamoDB?

The putItem function creates an entirely new item or replaces an existing item from a DynamoDB table. So, unlike the updateItem function, you can not use the putItem function to update existing items. The behavior of the putItem function is determined by the primary key you provide.

Can we do batch update in DynamoDB?

A bulk (batch) update refers to updating multiple rows belonging to a single table. However, DynamoDB does not provide the support for this.


1 Answers

Use a literal instead:

rc = table.update_item(Key={ 'username' : user }, 
    UpdateExpression="set list[" + itemnum + "].field = :nd",
    ExpressionAttributeValues={
        ':nd': data,
    },
    ReturnValues="UPDATED_NEW"
);
like image 175
André Werlang Avatar answered Oct 10 '22 19:10

André Werlang