Update nested map dynamodb

2020-05-23 06:49发布

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.

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

3条回答
We Are One
2楼-- · 2020-05-23 07:05

Updated Answer - based on updated question statement

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.

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

Then your query would be:

table.update_item(
  Key={
    'User': 'test'
  },
  UpdateExpression="SET #inv.#brand.#loc = :locVal",
  ExpressionAttributeNames={
    '#inv': 'inventory',
    '#loc': 'location',
    '#brand': 'opel'
  },
  ExpressionAttributeValues={
    ':locVal': 'in place three',
  },
)

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

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.

查看更多
劫难
3楼-- · 2020-05-23 07:18

You can update the nested map as follow:

  1. First create and empty item attribute of type map. In the example graph is the empty item attribute.

    dynamoTable = dynamodb.Table('abc')
    dynamoTable.put_item(
        Item={
            'email': email_add,
            'graph': {},
        }
    
  2. Update nested map as follow:

    brand_name = 'opel'
    DynamoTable = dynamodb.Table('abc')
    
    dynamoTable.update_item(
        Key={
            'email': email_add,
        },
        UpdateExpression="set #Graph.#brand= :name, ",
        ExpressionAttributeNames={
            '#Graph': 'inventory',
            '#brand': str(brand_name),
        },
        ExpressionAttributeValues = {
            ':name': {
                "location": "in place two",
                'tag': {
                    'graph_type':'a3',
                    'graph_title': 'f5'
                } 
            }
    
查看更多
家丑人穷心不美
4楼-- · 2020-05-23 07:26

Updating Mike's answer because that way doesn't work any more (at least for me).

It is working like this now (attention for UpdateExpression and ExpressionAttributeNames):

table.update_item(
  Key={
    'User': 'test'
  },
  UpdateExpression="SET inv.#brand.loc = :locVal",
  ExpressionAttributeNames={
    '#brand': 'opel'
  },
  ExpressionAttributeValues={
    ':locVal': 'in place three',
  },
)

And whatever goes in Key={}, it is always partition key (and sort key, if any).

EDIT: Seems like this way only works when with 2 level nested properties. In this case you would only use "ExpressionAttributeNames" for the "middle" property (in this example, that would be #brand: inv.#brand.loc). I'm not yet sure what is the real rule now.

查看更多
登录 后发表回答