Update nested map dynamodb(更新嵌套地图 dynamodb)
问题描述
我有一个 dynamodb 表,其中包含一个包含嵌套地图的属性,我想更新一个特定的库存项目,该项目通过过滤器表达式进行过滤,从而生成该地图中的单个项目.
I have a dynamodb table with an attribute containing a nested map and I would like to update a specific inventory item that is filtered via a filter expression that results in a single item from this map.
如何编写更新表达式将位置更新为就地三"名称=opel 的项目,标签包括x1";(也可能是 f3)?这应该只是更新第一个列表元素的位置属性.
How to write an update expression to update the location to "in place three" of the item with name=opel,tags include "x1" (and possibly also f3)? This should just update the first list elements location attribute.
{
"inventory": [
{
"location": "in place one", # I want to update this
"name": "opel",
"tags": [
"x1",
"f3"
]
},
{
"location": "in place two",
"name": "abc",
"tags": [
"a3",
"f5"
]
}],
"User" :"test"
}
推荐答案
更新的答案 - 基于更新的问题陈述
您可以使用更新表达式 这样只有项目的一部分会得到更新(即 DynamoDB 会将补丁应用到您的项目中),但是,由于 DynamoDB 是一个文档数据库,所有操作(Put、Get、Update、Delete 等)都会被更新.) 对整个项目进行处理.
You can update attributes in a nested map using update expressions such that only a part of the item would get updated (ie. DynamoDB would apply the equivalent of a patch to your item) but, because DynamoDB is a document database, all operations (Put, Get, Update, Delete etc.) work on the item as a whole.
因此,在您的示例中,假设 User
是分区键并且没有排序键(在该示例中我没有看到任何可能是排序键的属性),更新请求可能如下所示:
So, in your example, assuming User
is the partition key and that there is no sort key (I didn't see any attribute that could be a sort key in that example), an Update request might look like this:
table.update_item(
Key={
'User': 'test'
},
UpdateExpression="SET #inv[0].#loc = :locVal",
ExpressionAttributeNames={
'#inv': 'inventory',
'#loc': 'location'
},
ExpressionAttributeValues={
':locVal': 'in place three',
},
)
也就是说,您必须知道项目架构是什么样的,以及项目中的哪些属性应该准确更新.
That said, you do have to know what the item schema looks like and which attributes within the item should be updated exactly.
DynamoDB 无法对子项进行操作.意思是,没有办法告诉 Dynamo 执行诸如 更新项目,设置 'inventory' 数组元素的 'location' 属性,这些元素的 'name' 属性等于 'opel'"
DynamoDB does NOT have a way to operate on sub-items. Meaning, there is no way to tell Dynamo to execute an operation such as "update item, set 'location' property of elements of the 'inventory' array that have a property of 'name' equal to 'opel'"
这可能不是您希望的答案,但它是今天可用的.您可以通过稍微更改架构来更接近您想要的内容.
This is probably not the answer you were hoping for, but it is what's available today. You may be able to get closer to what you want by changing the schema a bit.
如果您需要按名称引用子项,可能存储如下内容:
If you need to reference the sub-items by name, perhaps storing something like:
{
"inventory": {
"opel": {
"location": "in place one", # I want to update this
"tags": [ "x1", "f3" ]
},
"abc": {
"location": "in place two",
"tags": [ "a3", "f5" ]
}
},
"User" :"test"
}
那么您的查询将是:
table.update_item(
Key={
'User': 'test'
},
UpdateExpression="SET #inv.#brand.#loc = :locVal",
ExpressionAttributeNames={
'#inv': 'inventory',
'#loc': 'location',
'#brand': 'opel'
},
ExpressionAttributeValues={
':locVal': 'in place three',
},
)
但是 YMMV 即使这样也有局限性,因为您只能通过名称识别库存项目(即,您仍然不能说使用标签 'x1' 更新库存"
But YMMV as even this has limitations because you are limited to identifying inventory items by name (ie. you still can't say "update inventory with tag 'x1'"
最终,您应该仔细考虑为什么需要 Dynamo 为您执行这些复杂的操作,而不是具体说明要更新的内容.
Ultimately you should carefully consider why you need Dynamo to perform these complex operations for you as opposed to you being specific about what you want to update.
这篇关于更新嵌套地图 dynamodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更新嵌套地图 dynamodb
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01