代码之家  ›  专栏  ›  技术社区  ›  Nico Müller

dynamodb/python updateitem嵌套映射中的追加项

  •  0
  • Nico Müller  · 技术社区  · 7 年前

    如何将项目追加到DyDoDB中的现有映射中

    发电机的结构如下

    {  
    "user": "xyz",   
    "itemdetails": [
        {
          "location": "67666",
          "name": "item1",
          "tags": [
            "k7866"
          ]
        },
        {
          "location": "45444",
          "name": "item12",
          "tags": [
            "ha23",
            "ju4532"
          ]
        }
    }
    

    我试过下面的但是

    函数;运算符:ADD,操作数类型:MAP“

    response = table.update_item(
        Key={
            'user': "xyz",
        },
        UpdateExpression = 'ADD #itemdetails :newItems',
        ExpressionAttributeNames = {
          '#itemdetails' : 'ids'
        },
        ExpressionAttributeValues = {
          ':newItems' : {"name":name, 
             "location":location,
             "tags":tags
            }
        },
        ReturnValues="UPDATED_NEW"
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Nico Müller    7 年前

    我现在就解决了

     UpdateExpression = 'SET itemdetails = list_append(itemdetails, :newitem)',
            ExpressionAttributeValues={
                ":newitem": [
                        {
                            "name":"TEST", 
                            "location":"TEST",
                            "tags":tags
                        }
                ]
            },
            ReturnValues="UPDATED_NEW"
        )