1

i need to update a list on my aws-dynamo database. i have created a table with partition key : email. then iam insert some email id to the table successfully. Now my table like this

email

[email protected]

[email protected]

[email protected]

Then, i tried to update the table with new key "details" and its value is a list. this is my code

var AWS = require("aws-sdk");

var params =
    {
        TableName: "manaftable1",
        Key: { email: "[email protected]" },
        UpdateExpression: "set #details = list_append (#details, :detailsinput)",
        ExpressionAttributeNames: {
            "#details": "details"
        },
        ExpressionAttributeValues: {
            ":detailsinput":{ "id": "1","mob": "978956" }
        }
    };


var docClient = new AWS.DynamoDB.DocumentClient();

docClient.update(params, function (err, data) {
    if (err)
        console.log(JSON.stringify(err, null, 2));
    else
        console.log(JSON.stringify(data, null, 2));
});

But i got error like this

{
  "message": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M",
  "code": "ValidationException",
  "time": "2016-10-26T11:04:60.756",
  "requestId": "SN0NPRHDFHUKBBJHOVI0DFHHRQNSO5AEMVJFFGHF9ASFHUAAJG",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}

i need response like this after updation

column1 : email

column2 : details

[email protected] | [{"id":"1","mob":"978956"},{"id":"2","mob":"767886"}]

[email protected] |

what is the issue related with my code?

2
  • Do you have details defined in the schema? Alternatively you can get the item from DDB, update it and then using put update it in DDB Commented Oct 26, 2016 at 11:41
  • i only define key email in schema. Commented Oct 26, 2016 at 11:54

3 Answers 3

2

As AWS documentation for list_append says:

The new element must be contained in a list, for example to add 2 to a list, the operand would be [2]

So you need to append an array of objects, not just an object:

var params =
    {
        TableName: "manaftable1",
        Key: { email: "[email protected]" },
        UpdateExpression: "set #details = list_append (#details, :detailsinput)",
        ExpressionAttributeNames: {
            "#details": "details"
        },
        ExpressionAttributeValues: {
            ":detailsinput": [{ "id": "1","mob": "978956" }]
        }
    };

If the property does not exist in the object, you can use if_not_exists operand:

var params =
    {
        TableName: "manaftable1",
        Key: { email: "[email protected]" },
        UpdateExpression: "set if_not_exists(#details, []) set #details = list_append (#details, :detailsinput)",
        ExpressionAttributeNames: {
            "#details": "details"
        },
        ExpressionAttributeValues: {
            ":detailsinput": [{ "id": "1","mob": "978956" }]
        }
    };
1
  • it will produce error { "message": "The provided expression refers to an attribute that does not exist in the item", "code": "ValidationException", "time": "2016-10-26T16:28:41.626Z", "requestId": "CAMLAN7JKISU9C7NSTKUPMHCDJVV4KQNSO5AEMVJF66Q9ASUAAJG", "statusCode": 400, "retryable": false, "retryDelay": 0 } Commented Oct 26, 2016 at 16:29
2

Use list_append() and if_not_exists() together to append to a potentially non-existent list column:

var params = {
  TableName: "manaftable1",
  Key: { email: "[email protected]" },
  UpdateExpression: "set #details = list_append(if_not_exists(#details, :empty_list), :detailsinput)",
  ExpressionAttributeNames: {
    "#details": "details"
  },
  ExpressionAttributeValues: {
    ":detailsinput": [{ "id": "1","mob": "978956" }],
    ":empty_list": []
  }
};
1

When the details attribute is NOT present in the item, the update expression should have just SET without the list_append.

UpdateExpression : "SET #details = :details"

When the details attribute is present in the item and the new update needs to add additional elements in the list, then list_append can be used to add the new element to the list.

UpdateExpression : "set #details = list_append (#details, :details)"

Not the answer you're looking for? Browse other questions tagged or ask your own question.