how can we update dynamodb table based on index(not based on primary has and range key).
I have a index created by name key_id-index
and hash is asset_id
and range is hit_id
.
i want to update the table based on key_id-index
because i wouldn't know those while updating.
var paramsu = {
TableName: 'asset',
//IndexName: 'key_id-index',
Key: { // The primary key of the item (a map of attribute name to AttributeValue)
asset_id: { S: 'a' },
hit_id: { S: 'h' }
// more attributes...
},
AttributeUpdates: { // The attributes to update (map of attribute name to AttributeValueUpdate)
key_id: {
Action: 'PUT', // PUT (replace)
// ADD (adds to number or set)
// DELETE (delete attribute or remove from set)
Value: { S: 'updated1' }
}
// more attribute updates: ...
},
ReturnValues: 'NONE', // optional (NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW)
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE)
};
db.updateItem(paramsu, function(err, data) {
if (err) console.log(err); // an error occurred
else console.log(data); // successful response
});
You can use UpdateItem to update any nonkey attributes. Show activity on this post.
A primary key is consists of a hash key and an optional range key. Hash key is used to select the DynamoDB partition. Partitions are parts of the table data. Range keys are used to sort the items in the partition, if they exist.
No item collection can exceed 10 GB, so it's possible to run out of space for a particular partition key value. When you add or update a table item, DynamoDB updates all local secondary indexes that are affected.
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.
You cannot write to a GSI.
Your only option is to first read from the GSI, get the attribute values that are are your keys (they are always projected onto the index, see GSI.Projections), and then write your data back to the table.
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