Reputation: 183
I have a DynamoDB Table and I want a particular value to be updated using multiple condition expression. Can we do that? My code:-
dynamodb = session.resource('dynamodb')
table = dynamodb.Table('gold-images')
response = table.update_item(
Key={
'AccountRegionOS' : AccOs,
'CreationDate' : cred
},
UpdateExpression="set is_active = :r",
ConditionExpression="CreationDate < :num",
ExpressionAttributeValues={
':num' : last_month,
':r': "No"
},
ReturnValues="UPDATED_NEW"
I want Condition expression to be
dynamodb = session.resource('dynamodb')
table = dynamodb.Table('gold-images')
response = table.update_item(
Key={
'AccountRegionOS' : AccOs,
'CreationDate' : cred
},
UpdateExpression="set is_active = :r",
ConditionExpression=("CreationDate < :num") & ("AMIID = :ami"),
ExpressionAttributeValues={
':num' : last_month,
':r': "No",
':ami' : i
},
ReturnValues="UPDATED_NEW"
Upvotes: 12
Views: 21544
Reputation: 794
For those who are using AWS lambda to update their DynamoDB table, here is an example:
const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();
const proxyResponseBody = ((statusCode, result) => {
let response = {
statusCode: statusCode,
body: JSON.stringify({ result: result }),
headers: { "Access-Control-Allow-Origin": "*" }
}
return response;
})
exports.handler = async (event) => {
// TODO implement
console.log(event);
const jsonData = JSON.parse(event.body)
const userId = event.requestContext.authorizer.claims.sub
const params = {
TableName : "Some_Table_Name",
Key: {
"ID": jsonData.tableKeyId,
},
UpdateExpression: "set someField = :newValue",
ConditionExpression: "userId = :userId",
ExpressionAttributeValues: {
":newValue": jsonData.newValue,
":userId": userId,
},
}
console.log(params)
let result = null
let statusCode = null
result = await documentClient.update(params, function(err, data) {
if (err) {
console.log(err)
statusCode = 500
return err
}else {
console.log(data);
statusCode = 200
return data
}
}).promise();
return proxyResponseBody(statusCode, result)
};
The table consist of a partition key "ID", "userId", and a "fieldName". This function is to update the value of "fieldName" of a row which have the matching userId.
Upvotes: 0
Reputation: 10547
The condition expression is a string and the logical operators are AND
, OR
and NOT
. So, you'll need to remove outer parentheses and replace &
with AND
:
ConditionExpression = "CreationDate < :num AND AMIID = :ami"
Upvotes: 19