36

in the boto3 documentation updating an item is described by this example:

table.update_item(
    Key={
        'username': 'janedoe',
        'last_name': 'Doe'
    },
    UpdateExpression='SET age = :val1',
    ExpressionAttributeValues={
        ':val1': 26
    }
)

But what am i supposed to do, if i want to update several values of the item. I couldn't find any information on this. Can anybody help? Thank you!

EDIT:

Ok, it seems to work like this, can anybody confirm wether this is the right way. Just to be sure i'm not doing anything totally wrong here.

table.update_item(
                    Key={
                        'id': item['id']
                    },
                    UpdateExpression='SET value1 = :val1, value2 = :val2',
                    ExpressionAttributeValues={
                        ':val1': someValue1,
                        ':val2': someValue2
                    }
                )

Thanks!

weka1
  • 737
  • 2
  • 9
  • 20
  • 3
    Yes that is correct. It's documented clearly here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html – Mark B Jun 09 '16 at 16:39

2 Answers2

25

Yes, that's the way to do. Even many actions can be done in single expression as shown here. For ex: multiple 'PUT' and 'DELETE' in single expression.

Snapshot showing the example from document

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Venkatesh Wadawadagi
  • 2,793
  • 21
  • 34
-1

For others finding this, theres a library you can use to help with the weird boto3 stuff like ExpressionAttributeValues. This way you can make normal developer/human calls.

https://github.com/rayepps/dynamof

db(update(
  table_name='users',
  key={ 'id': item['id'] },
  attributes={
      'age': 26,
      'name': 'Carl'
  }))

disclaimer: I wrote it

rayepps
  • 2,072
  • 1
  • 12
  • 22