本文介紹如何通過 Node.js SDK 更新Table Store資料表中的單行資料,您可以更新屬性列的值、添加屬性列、刪除屬性列的某個版本或整個屬性列。
前提條件
方法說明
updateRow: function updateRow(params, callback)範例程式碼
以下範例程式碼用於修改 test_table 表中主索引值為 row1 的行資料,將屬性列 col1 的值修改為 changed_val1。
var params = {
tableName: 'test_table',
primaryKey: [{ 'id': 'row1' }],
// 更新行資料時必須指定更新條件 (TableStore.RowExistenceExpectation.IGNORE,表示不做行存在性判斷)
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null)
};
params.updateOfAttributeColumns = [
{ 'PUT': [{ 'col1': 'changed_val1' }] }
]
client.updateRow(params, function (err, data) {
if (err) {
console.log('Update row failed with error:', err);
return;
}
console.log('RequestId: ', data.RequestId);
console.log('Read CU Cost: ', data.consumed.capacityUnit.read);
console.log('Write CU Cost: ', data.consumed.capacityUnit.write);
});您也可以參照範例程式碼進行以下行資料操作。
添加一個屬性列。
params.updateOfAttributeColumns = [ { 'PUT': [{ 'col2': 'val2' }] } ]設定屬性列資料版本號碼。
params.updateOfAttributeColumns = [ { 'PUT': [{ 'col2': 'val2', 'timestamp': Date.now() }] } ]刪除屬性列指定版本的資料。
params.updateOfAttributeColumns = [ { 'DELETE': [{ 'col2': TableStore.Long.fromNumber(1496826473186) }] }, ]刪除整個屬性列資料。
params.updateOfAttributeColumns = [ { 'DELETE_ALL': ['col2'] } ]