Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS DynamoDB - How to achieve in 1 call: Add value to set, if set exists - or else instantiate set with value?

I have a users table, there is an attribute called friends, which will be a set of the id's of all the user's friends.

Initially I tried instantiating the friends attribute to an empty set when the user is created, but I get an error that you can't have an empty attribute.

So the only solution I could find if someone has no friends yet is to read the attribute on the user, if it does not exist, SET the attribute to a [new] set with the friend they are adding. If it does exist, then just perform an update with an ADD, which adds the new friend to the set.

I don't want to have to make two calls to AWS for this.

Is there a way to create the set if it doesn't exist, and if it does, add to it - all in just 1 call?

like image 772
ryanm Avatar asked Feb 08 '17 01:02

ryanm


People also ask

What is the use of add new attribute method in DynamoDB?

ADD - Causes DynamoDB to create an item with the supplied primary key and number (or set of numbers) for the attribute value. The only data types allowed are Number and Number Set.

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

How do you update multiple items in a DynamoDB table?

(If you want to modify multiple items, you must use multiple operations.) With the DynamoDB API, you use the UpdateItem action to modify a single item. You must specify the Key attributes of the item to be modified and an UpdateExpression to specify attribute values.

What is the API call to retrieve multiple items from a DynamoDB table?

The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items.


1 Answers

For SET data type (from DynamoDB API Reference):

ADD - If the attribute does not already exist, then the attribute and its values are added to the item. If the attribute does exist, then the behavior of ADD depends on the data type of the attribute:

If the existing data type is a set, and if the Value is also a set, then the Value is added to the existing set. (This is a set operation, not mathematical addition.) For example, if the attribute value was the set [1,2], and the ADD action specified [3], then the final attribute value would be [1,2,3]. An error occurs if an Add action is specified for a set attribute and the attribute type specified does not match the existing set type. Both sets must have the same primitive data type. For example, if the existing data type is a set of strings, the Value must also be a set of strings. The same holds true for number sets and binary sets.

Example:-

First update:-

The country attribute is not present in the table. The updateItem created the new attribute country with the values (IN, UK) provided.

var params = {
        TableName : "Movies",
        Key : {
            "yearkey" : 2014,
            "title" : "The Big New Movie 2"
        },
        UpdateExpression : "ADD country :countries",            
         ExpressionAttributeValues: {
                ':countries': docClient.createSet(["IN", "UK"])             
            },
        ReturnValues : "UPDATED_NEW"
    };

Second update:-

This time updateItem added the new value "US" and ignored the existing value "IN".

var params = {
        TableName : "Movies",
        Key : {
            "yearkey" : 2014,
            "title" : "The Big New Movie 2"
        },
        UpdateExpression : "ADD country :countries",            
         ExpressionAttributeValues: {
                ':countries': docClient.createSet(["IN", "US"])             
            },
        ReturnValues : "UPDATED_NEW"
    };
like image 157
notionquest Avatar answered Sep 20 '22 03:09

notionquest