how can we update dynamodb table based on index(not based on primary has and range key)

我的未来我决定 提交于 2021-02-07 13:52:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!