DynamoDB: SET list_append not working with aws sdk

I need to add a row to a rowset in a dynamodb table using the appropriate key. This is the Update expression that I use to execute updateItem:

var params = { "TableName" : tableName, "Key": { "ID": { S: "20000" } }, "UpdateExpression" : "SET #attrName = list_append(#attrName, :attrValue)", "ExpressionAttributeNames" : { "#attrName" : "entries" }, "ExpressionAttributeValues" : { ":attrValue" : {"SS":["000989"]} } }; 

This works when I do updateItem () using aws cli. But when using aws-sdk in nodejs I get an error message:

 Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: M\n 

Any help? Thanks

+7
amazon-web-services amazon-dynamodb aws-sdk
source share
4 answers

list_append can be read as a concatenation operation. You just give him two lists.

 "UpdateExpression" : "SET #attrName = list_append(#attrName, :attrValue)", "ExpressionAttributeNames" : { "#attrName" : "entries" }, "ExpressionAttributeValues" : { ":attrValue" : ["000989"] } 

It is worth remembering that lists (and maps) in DynamoDB are not printed and can store arbitrary data.

Side Note: Armed with this knowledge, the documentation for adding to the top of the list now makes sense:

list_append (operand, operand)

This function computes a list with the addition of a new item. You can add a new element to the beginning or end of the list by changing the order of the operands.

+17
source share

There the answer to this question is accepted, which helped me with part of this problem. However, we usually want to update lists with additional objects , rather than rows. For this, I found it helpful to avoid using ExpressionAttributeNames if possible.

1) Make sure the value in your element in the DynamoDB table is a list. 2) Make sure you go to the list of objects (even if you only have one), and not a simple object

  UpdateExpression: "set pObj.cObj= list_append(pObj.cObj, :obj)", ExpressionAttributeValues: { ":obj": [ {myObject: { property1: '', property2: '', property3: '', }} ] }, 
+2
source share

I thought I would just throw it away as another option to add or add an β€œobject” to the list. This map added the item to the list and worked well for me:

 var upsertExpr = (obj.comments == undefined) ? " :attrValue" : "list_append(#attrName, :attrValue)"; var params = { TableName: 'tableName', Key: { 'id': {'S': id}, }, UpdateExpression : "SET #attrName = " + upsertExpr, ExpressionAttributeNames : { "#attrName" : "comments" }, ExpressionAttributeValues : { ":attrValue" : { "L": [ { "M" : { "comment": {"S": comment}, "vote": {"N": vote.toString()} } } ] } } }; 
+1
source share

Maybe this will help someone. I struggled with updating the list and received the same error message as the original poster. I managed to solve my problem when I finally understood the documentation (see the example of adding items to the list here http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.ADD )

indicates that: 1) that "list_append takes two lists as input and adds a second list to the first." and 2) that ExpressionAttributeValues ​​is a list! eg:

 { ":vals": { "L": [ { "S": "Screwdriver" }, {"S": "Hacksaw" } ] } 

}

Good luck

0
source share

All Articles