问题
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
});
回答1:
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.
来源:https://stackoverflow.com/questions/32886247/how-can-we-update-dynamodb-table-based-on-indexnot-based-on-primary-has-and-ran