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?
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.
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.
(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.
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.
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"
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With