I have a table sorted by user id, each object contains a list of objects that have their own id plus two attributes. Is it possible to use update_item to update either one of those attributes, using user_id, and obj_id. The only way I have found to do is to use GET and then PUT, which feels like it could be more expensive.
This is and example entry in my table:
{'id': '0',
'commands': [{'id': '0', 'command': '', actions: []}, {'id': '1', 'command': '', actions: []}]
}
Say I wanted to update command with id 0 on user_id 0 to have a different list of actions I figure I can do something like this:
table = dynamodb.Table('commands')
user_id = '0'
command_id = '0'
document = table.get_item(Key={"id": user_id})['Item']
for command in document['commands']:
if command['id'] == command_id:
command['actions'] = ['new actions']
break
table.put(Item=document, ReturnValues="NONE")
But is there a way of doing it with update_item that may be more efficient?
The only thing I can find in the docs is to use list_append in the Expression, but I don't want to append a new item I want to edit an existing one.
I've found these two questions as reference, the first one is where I got the solution above, but the question I'm asking here hasn't been answered, can I use update_item to do this?