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!
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.
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.
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.
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.
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"
}
};
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