Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add non existing element to array in DynamoDB

Im trying to update items attribute that is a list of strings. Can I update (append) the attribute only if it not exist . Kind of list_append & if_not_exists.

var params = { ...

UpdateExpression: 

'SET friends = list_append(if_not_exists(friends, :empty_list), :new_friend)',

ExpressionAttributeValues:{
    ":new_friend": [{"S":"Bobo"}],
    ":empty_list" :[]
 } };

this is not working, is there a way? so if Bobo still not in my "friends" list it will append it

like image 319
Alex Portnoy Avatar asked Jun 19 '16 17:06

Alex Portnoy


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.

How do I add a field in DynamoDB?

We can store the values in DynamoDb in 2 ways, (i) In an RDBMS Type of Structure for the DynamoDB, we can add a new Coulmn by executing the same command keeping the "new Column" entry within which the Records in the Existing Table has been created.

What is atomic counter in DynamoDB?

You can use the UpdateItem operation to implement an atomic counter—a numeric attribute that is incremented, unconditionally, without interfering with other write requests. (All write requests are applied in the order in which they were received.) With an atomic counter, the updates are not idempotent.

Can we store array in DynamoDB?

DynamoDB can store JSON data , also the data format you wish to insert.


1 Answers

You can use the "not contains" and "list_append" for your requirement.

The below code inserts the new friend to list if the friend is NOT already present in the list.

If the friend is already present in the list, it would throw "conditional request failed".

Error message if condition fails:-

Unable to update item. Error JSON: {
  "message": "The conditional request failed",
  "code": "ConditionalCheckFailedException",
  "time": "2016-06-22T08:18:36.483Z",
  "requestId": "86805965-240b-43e0-8fdc-77fb9ae1b15c",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}

The below code works fine. It has been tested successfully.

Code Sample:

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

AWS.config.update({
    region : "us-west-2",
    endpoint : "http://localhost:8000"
});

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

var table = "users";

var userid = 1;
var friendId = ["f4"];
var friendIdStr = "f4";


//Add the new DOCUMENT TYPE attribute to the table
var params = {
    TableName : table,
    Key: {
        "id" : userid 
    },
    "UpdateExpression": "set friends = list_append (friends, :friendId)",
    "ConditionExpression": "not contains (friends, :friendIdStr)",
    "ExpressionAttributeValues": { 
        ":friendId": friendId,
        ":friendIdStr" : friendIdStr
    },
    "ReturnValues" : "UPDATED_NEW"
};

console.log("Updated an item...");
docClient.update(params, function(err, data) {
    if (err) {
        console.error("Unable to update item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("Updated item:", JSON.stringify(data, null, 2));
    }
});
like image 138
notionquest Avatar answered Sep 19 '22 02:09

notionquest