Reputation: 11
I want to update existing item of dynamoDB table using condition equivalent to like (%) condition of sql
opIdUserId in my dynamodb is Primary partition key. I want to update status to COMP for opIdUserId field value starting from or contains 123.
this is what I am trying currently but researched and found I can not use KeyConditionExpression for update.
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});
var table = "tablename";
exports.handler = function(event, context, callback){
var params = {
TableName: table,
KeyConditionExpression: "begins_with(opIdUserId, :i)",
UpdateExpression: "set operationStatus = :o",
ExpressionAttributeValues:{
":o":"COMP",
":i":"123-"
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) {
callback(err, null);
} else {
callback(null,data);
}
});
}
Please suggest how I can update my item in dynamoDB with specific condition.
Upvotes: 0
Views: 1690
Reputation: 2540
You should create a Global Secondary Index on your table to query
for the items that start with 123
using a KeyConditionExpression
.
var params = {
TableName: table,
KeyConditionExpression: "id = :id AND begins_with(opIdUserId, :opIdUserId)",
UpdateExpression: "set operationStatus = :operationStatus",
ExpressionAttributeValues:{
":opIdUserId":"123-",
":id": "something"
}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.query(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
Bare in mind that you can't run a query using BEGINS_WITH
on a partition key
, only on a sort key
.
Then, you can use the update
method over every individual element using the partition and sort key
for the table, updating the operationStatus
.
If you provide me with information regarding your DynamoDB
table, I could help you create the GSI
.
Upvotes: 1