Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an item in Dynamodb of type String Set (SS)?

I have created a attribute of type String Set. When I create the Item and assign an attribute of type SS everything works. But when I try to update this attribute, the data type changes to a list ("L").

I try this:

qw = new AWS.DynamoDB.DocumentClient();

var params = {
  TableName : "myTable",
  Key: {
    "id": somekey
  },
  UpdateExpression: "set ssvar= :arrp",
  ExpressionAttributeValues: {
     ":arrp": [ "test", "test2" ]
  }
};

qw.update (etc.)

This leads to a change in datatype in dynamodb and in stead of a string set I get a list:

"ssvar": {
            "L": [
                {
                    "S": "test"
                }, 
                {
                    "S": "test2"
                }
            ]
        }

I have tried all kinds of solutions like below but always my datatype gets changed.

  ExpressionAttributeValues: {
     ":arrp": 
        "SS": [ "test", "test2" ]
  }

How can I update an attribute of type string set?

like image 727
Jose Avatar asked May 12 '16 18:05

Jose


People also ask

How do I update a DynamoDB item?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

What is SS in DynamoDB?

SS (string set) type, NS (number set) type, or BS (binary set) type. The DynamoDBTypeConverter interface lets you map your own arbitrary data types to a data type that is natively supported by DynamoDB.

What is string set in DynamoDB?

DynamoDB supports types that represent sets of number, string, or binary values. All the elements within a set must be of the same type. For example, an attribute of type Number Set can only contain numbers; String Set can only contain strings; and so on.

Can we update sort key value in DynamoDB?

Can we update the sort key in DynamoDB? No, you can not update the sort key after the table is provisioned. However, you can create a new table and put the existing data in the newly created table, and delete the old table.


2 Answers

As of September 2015, there is a createSet function in the DocumentClient that you can use for this.

UPDATE - added example

I've modified your example code to use this function:

qw = new AWS.DynamoDB.DocumentClient();

var params = {
  TableName : "myTable",
  Key: {
    "id": somekey
  },
  UpdateExpression: "set ssvar= :arrp",
  ExpressionAttributeValues: {
     ":arrp": qw.createSet([ "test", "test2" ])
  }
};

qw.update (etc.)
like image 81
TorAL Avatar answered Sep 23 '22 13:09

TorAL


This is an artifact of using the DocumentClient - StringSet is not a JSON type.

The DocumentClient converts a StringSet to the Array native JavaScript type: https://github.com/aws/aws-sdk-js/blob/master/lib/dynamodb/converter.js#L61. Then the client serializes the native JavaScript Array as a DynamoDB List type: https://github.com/aws/aws-sdk-js/blob/master/lib/dynamodb/converter.js#L12.

If you want to use the StringSet type, you can use the low-level API: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

like image 20
Ben Schwartz Avatar answered Sep 24 '22 13:09

Ben Schwartz