How can i update an object

0

Hello, i got a problem

I use DynamoDB with Serverless.
I have a request that allows me to update an item from my table. But one of the objects of this item is like this :

"example1":{
"example2":{
"name1" : "blabla"
}
}
I want to add values in example2, like "name2", "name3" ...
My request with UpdateExpression and ExpressionAttributeValues looks like this :

let params = {
...
UpdateExpression: "SET example1.example2.name2 = :name2, example1.example2.name3 = :name3",
ExpressionAttributeValues: {
":name2": name2,
":name3" : name3
},
...
ReturnValues: 'UPDATED_NEW'
}
And when i run my request i got an answer like this "message":"The document path provided in the update expression is invalid for update"

Can you help me if you already have something like this ?

Thanks

Best regards

dc-two
已提问 5 年前2311 查看次数
3 回答
0

Hi, I am reviewing this now, and will send you an update once I have more information. Thank you.

AWS
已回答 5 年前
0

I tried the following:

#Existing item json:

c:\PADMA\UTILS\SCRIPTS>aws dynamodb scan --table-name example
{
    "Items": [
        {
            "pk": {
                "N": "1"
            },
            "example1": {
                "M": {
                    "example2": {
                        "M": {
                            "name2": {
                                "S": "a"
                            },
                            "name1": {
                                "S": "blabla"
                            }
                        }
                    }
                }
            },
            "sk": {
                "S": "key"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

#Update command:

table.update_item(
    Key={
        'pk': pk,
        'sk': sk
    },
    UpdateExpression="set example1.example2.name3=:x",
    ExpressionAttributeValues={
        ':x': "new value"
    },
    ReturnValues="UPDATED_NEW"
)

#Updated item json:

c:\PADMA\UTILS\SCRIPTS>aws dynamodb scan --table-name example
{
    "Items": [
        {
            "pk": {
                "N": "1"
            },
            "example1": {
                "M": {
                    "example2": {
                        "M": {
                            "name1": {
                                "S": "blabla"
                            },
                            "name2": {
                                "S": "a"
                            },
                            "name3": {
                                "S": "new value"
                            }
                        }
                    }
                }
            },
            "sk": {
                "S": "key"
            }
        }
    ],
    "Count": 1,
    "ScannedCount": 1,
    "ConsumedCapacity": null
}

Is this what you are trying to do? I am getting the expected results. I followed this tutorial: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.01.html
Can you try this out. And let me know if you are still having trouble with this?

AWS
已回答 5 年前
0

Yes, that's work

My mistake was that one of my values ​​had to be created in a new object, but since the object did not exist, it did not understand the path.

Thanks for your help

dc-two
已回答 5 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则