更新嵌套地图dynamodb [英] Update nested map dynamodb

查看:88
本文介绍了更新嵌套地图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.

如何编写更新表达式以将名称更新为name = opel的项目的位置替换为"inplace 3",标签包括"x1"(可能还包括f3)? 这应该只更新第一个列表元素的location属性.

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" 
 } 

推荐答案

更新后的答案-基于更新后的问题陈述

Updated Answer - based on updated question statement

您可以使用更新表达式,以便仅更新项目的一部分(即,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执行诸如更新项目之类的操作,设置具有'name'等于'opel'的属性的'inventory'数组元素的'location'属性. /em>

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆