Boto3 DynamoDB 是否可以对对象列表执行 update_item
Boto3 DynamoDB is it possible to do update_item on a list of objects
我有一个按用户 ID 排序的 table,每个对象都包含一个对象列表,这些对象具有自己的 ID 和两个属性。是否可以使用 update_item 来更新其中一个属性,使用 user_id 和 obj_id。我发现唯一的办法就是先GET再PUT,感觉会比较贵
这是我 table 中的示例条目:
{'id': '0',
'commands': [{'id': '0', 'command': '', actions: []}, {'id': '1', 'command': '', actions: []}]
}
说我想在 user_id 0 上更新 id 为 0 的命令以获得不同的操作列表我想我可以做这样的事情:
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")
但是 update_item 有没有更有效的方法呢?
我在文档中唯一能找到的是在表达式中使用 list_append,但我不想附加新项目我想编辑现有项目。
我找到了这两个问题作为参考,第一个是我上面的解决方案,但是我在这里问的问题没有得到回答,我可以使用update_item来做这个?
Boto3 DynamoDB update list attributes for an item
就相关文档而言,目前没有 BatchUpdate
API 可用。
我能看到的只有这个BatchWriteItem适用于put
和delete
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.
BatchWriteItem cannot update items. To update items, use the UpdateItem action.
我有一个按用户 ID 排序的 table,每个对象都包含一个对象列表,这些对象具有自己的 ID 和两个属性。是否可以使用 update_item 来更新其中一个属性,使用 user_id 和 obj_id。我发现唯一的办法就是先GET再PUT,感觉会比较贵
这是我 table 中的示例条目:
{'id': '0',
'commands': [{'id': '0', 'command': '', actions: []}, {'id': '1', 'command': '', actions: []}]
}
说我想在 user_id 0 上更新 id 为 0 的命令以获得不同的操作列表我想我可以做这样的事情:
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")
但是 update_item 有没有更有效的方法呢?
我在文档中唯一能找到的是在表达式中使用 list_append,但我不想附加新项目我想编辑现有项目。
我找到了这两个问题作为参考,第一个是我上面的解决方案,但是我在这里问的问题没有得到回答,我可以使用update_item来做这个?
Boto3 DynamoDB update list attributes for an item
就相关文档而言,目前没有 BatchUpdate
API 可用。
我能看到的只有这个BatchWriteItem适用于put
和delete
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.
BatchWriteItem cannot update items. To update items, use the UpdateItem action.