Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate multiple clauses in a DynamoDB Update Expression

Per the AWS Docs:

An update expression consists of one or more clauses. Each clause begins with a SET, REMOVE, ADD or DELETE keyword. You can include any of these clauses in an update expression, in any order. However, each action keyword can appear only once.

I'm having trouble getting the syntax right for SET and REMOVE in one update_expression:

params = {
           key: {
             'id' => { s: '123'  }
           },
           table_name: 'customers'
           expression_attribute_names: {
             '#ADR' => 'address'
             '#ON' => 'order_num'
           },
           expression_attribute_values: {
             ':a' => { s: '123 Main St' }
           },
           update_expression: 'REMOVE #ON, SET #ADR = :a',
         }
  dynamodb = Aws::DynamoDB::Client.new(region: 'us-east-1',       
                                       simple_attributes: false)
  dynamodb.update_item(params)

I also tried the following:

update_expression: 'SET #ADR = :a, REMOVE #ON' => Invalid UpdateExpression: Syntax error; token: "REMOVE", near: ", REMOVE #ON" (Aws::DynamoDB::Errors::ValidationException)

update_expression: ['SET #ADR = :a', 'REMOVE #ON'] => expected params[:update_expression] to be a String, got value ['SET #ADR = :a', 'REMOVE #ON'] (class: Array) instead. (ArgumentError)

Can anyone point me to the right docs?

like image 262
cameck Avatar asked Aug 07 '17 18:08

cameck


People also ask

How do you update multiple items in a DynamoDB table?

As you noted, DynamoDB does not support a batch update operation. You would need to query for, and obtain the keys for all the records you want to update. Then loop through that list, updating each item one at a time.

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

Can we update sort key value in 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 Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.


1 Answers

I was making it too complicated.
This is the answer:

update_expression: 'SET #ADR = :a REMOVE #ON'

No delimiter character needed besides the Clause keyword.

like image 82
cameck Avatar answered Oct 05 '22 06:10

cameck