Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB Add new Map to List

I am trying to add a new Map to a List (Comments) in DynamoDB, but I can't seem to get it right. Below is the simple structure of the table:

{
  "Comments": [
    {
      "Body": "This is the first comment",
      "Username": "admin"
    },
    {
      "Body": "This is the second comment",
      "Username": "Jenny"
    },
    {
      "Body": "This is the third comment",
      "Username": "Bob"
    }
  ],
  "PostId": "RY28q1AxhR9Qi2VjrDdUdo5bPXBybUg2"
}

PHP Code:

$response = $ddb->updateItem (
              [
                'TableName'     => 'comments',
                  'Key'         => [
                    'PostId'    => ['S' => $request->input('postid')]
                  ],
              "ExpressionAttributeNames" => ["#theList" => "Comments"],
              "ExpressionAttributeValues" => [
                  ':addVal' => [
                      'M' =>  [
                        'Username' => ['S' => 'MrSmith'],
                        'Body' => ['S' => 'Hello this is a comment'],
                      ]
                  ]
              ],
              'UpdateExpression' => 'SET #theList = list_append(:addVal, #theList)',
          ]);

I get this error:

{
"result": 1,
"error": {
    "message": "Error executing \"UpdateItem\" on \"https://dynamodb.us-east-1.amazonaws.com\"; AWS HTTP error: Client error: 400 ValidationException (client): Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M - {\"__type\":\"com.amazon.coral.validate#ValidationException\",\"message\":\"Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M\"}",
    "status_code": "ValidationException"
}
like image 273
YaseenTenaka Avatar asked Nov 13 '15 08:11

YaseenTenaka


2 Answers

I have been with the same problem and thought is somewhat explained here and there, this is what finally worked out

$res =$this->dynamodb->updateItem([
            'TableName' => 'your_table',
            'Key' => [
                    'key1' => ['N' =>  $detail1,
                    'key2' => ['S' => $detail2,
            "ExpressionAttributeNames" => ["#theList" => "my_list_iten_name"],
            "ExpressionAttributeValues" => [
                ':empty_list' => ['L'=>[]],
                ':addVal' => [
                    'L' => [
                        [
                            'M' =>  [
                                'val1' => ['S' => 'one'],
                                'val2' => ['S' => 'two!!!'],
                            ]
                        ],
                        [
                            'M' =>  [
                                'val1' => ['S' => 'one1'],
                                'val2' => ['S' => 'two2!!!'],
                            ]
                        ]
                    ]
                ]
            ],
            'UpdateExpression' => 'SET #theList = list_append(if_not_exists(#theList, :empty_list), :addVal)',
        ]);
like image 52
tyoc213 Avatar answered Oct 21 '22 05:10

tyoc213


as a javascript developer I solved my problem when I wrapped my Object (Map) in an array (List), and it looks like this.

var params = {
    TableName: 'tableName',
    Key: {
        Id: "myId",
    },
    UpdateExpression: 'SET theList = list_append(theList, :value)',
    ExpressionAttributeValues: {
        ':value': [{test: 'data'}]
    },
    ReturnValues: 'UPDATED_NEW',
};
docClient.update(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

As I understood here list_append is used to append an list to another and I'm afraid it's the only way to do it. Based on what you wrote you can try:

"ExpressionAttributeValues" => [
              ':addVal' => [
                  'L' => [[
                    'M' =>  [
                        'Username' => ['S' => 'MrSmith'],
                        'Body' => ['S' => 'Hello this is a comment'],
                    ]
                  ]]
              ]
          ],
like image 44
AlexScr Avatar answered Oct 21 '22 06:10

AlexScr