Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB: Value provided in ExpressionAttributeNames unused in expressions: keys: {#date}

I am trying to update an item by changing value of isRelevant to true:

       var params = {
              TableName: "test",
              Key: {
                  "#date": data.Items[i].date.N,
                  "accountid": data.Items[i].accountid.S
              },
              UpdateExpression: "set #uu = :x",
              ExpressionAttributeValues: {
                  ":x": {"BOOL": false}
              },
              ExpressionAttributeNames: {
                   '#uu': "isRelevant",
                   '#date': "date"
              }
          };
          
          docClient.update(params, function(err, data) {
              if (err) console.log(err);
              else {
                  console.log('worked');
              }
          });

What is wrong in this code? I tried all possible ways, but still not working!

like image 566
AmazingDayToday Avatar asked Oct 23 '18 21:10

AmazingDayToday


People also ask

What is Expressionattributenames in DynamoDB?

An expression attribute name is a placeholder that you use in an Amazon DynamoDB expression as an alternative to an actual attribute name. An expression attribute name must begin with a pound sign ( # ), and be followed by one or more alphanumeric characters.

What is expression attribute values?

Expression attribute values in Amazon DynamoDB are substitutes for the actual values that you want to compare—values that you might not know until runtime. An expression attribute value must begin with a colon ( : ) and be followed by one or more alphanumeric characters.

What is projection expression in DynamoDB?

Amazon DynamoDB returns all the item attributes by default. To get only some, rather than all of the attributes, use a projection expression. A projection expression is a string that identifies the attributes that you want. To retrieve a single attribute, specify its name.


2 Answers

You don't need to use attribute name mapping for your keys in dynamo.

At the moment you params read like you have a key called '#date' and that you've randomly declared an attribute called '#date' that you're not using.

Instead try:

var params = {
  TableName: "test",
  Key: { "date": data.Items[i].date, "accountid": data.Items[i].accountid },
  UpdateExpression: "set #uu = :x",
  ExpressionAttributeValues: { ":x": false },
  ExpressionAttributeNames: { '#uu': "isRelevant" }
};

Also- when using DynamoDB.DocumentClient, you should use JSON values and it'll deal with the marshalling info the dynamo typed format.

like image 165
thomasmichaelwallace Avatar answered Oct 09 '22 13:10

thomasmichaelwallace


If your date id not Key in dynamoDB than you have to use in ConditionExpression

Example :

let queryParams = {
      TableName: "test",
      Key: {
        'accountid': data.Items[i].accountid
      },
      UpdateExpression: "set #uu = :x",
      ConditionExpression: 'date = :date',
      ExpressionAttributeValues: {
        ':x': false,
        ':date': data.Items[i].date,
      },
      ExpressionAttributeNames: {
        '#uu': "isRelevant"
   }
};
like image 40
IftekharDani Avatar answered Oct 09 '22 12:10

IftekharDani