简体   繁体   English

Boto3 DynamoDB 是否可以在对象列表上执行 update_item

[英]Boto3 DynamoDB is it possible to do update_item on a list of objects

I have a table sorted by user id, each object contains a list of objects that have their own id plus two attributes.我有一个按用户 ID 排序的表,每个 object 都包含一个对象列表,这些对象具有自己的 ID 和两个属性。 Is it possible to use update_item to update either one of those attributes, using user_id, and obj_id.是否可以使用 update_item 来更新其中任一属性,使用 user_id 和 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.我发现的唯一方法是使用 GET 然后 PUT,感觉它可能更昂贵。

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:假设我想在 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")    

But is there a way of doing it with update_item that may be more efficient?但是有没有一种方法可以更有效地使用 update_item 呢?

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.我在文档中唯一能找到的是在表达式中使用 list_append,但我不想 append 一个新项目,我想编辑一个现有项目。

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?我找到这两个问题作为参考,第一个是我在上面得到解决方案的地方,但是我在这里问的问题没有得到回答,我可以使用 update_item 来做到这一点吗?

Boto3 DynamoDB update list attributes for an item Boto3 DynamoDB 更新项目的列表属性

How to append a value to list attribute on AWS DynamoDB? 如何 append 列出 AWS DynamoDB 上的属性的值?

As far as the documentation concerned, there is no BatchUpdate API available as of now.就相关文档而言,目前还没有可用的BatchUpdate API。

Only one I can see is this BatchWriteItem which is applicable to put and delete我只能看到这个BatchWriteItem适用于putdelete

The BatchWriteItem operation puts or deletes multiple items in one or more tables. BatchWriteItem 操作在一个或多个表中放置或删除多个项目。 A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests.对 BatchWriteItem 的一次调用可以写入多达 16 MB 的数据,其中可以包含多达 25 个放置或删除请求。 Individual items to be written can be as large as 400 KB.要写入的单个项目可以大到 400 KB。

BatchWriteItem cannot update items. BatchWriteItem 无法更新项目。 To update items, use the UpdateItem action.要更新项目,请使用 UpdateItem 操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM