不能更新Dynamo Db表,得到ValidationException

我需要通过仅使用分区键来更新我的Dynamo数据库表。 但我有validation的exeption。 我创build了一个3字段的表。

  1. id(分区键)
  2. 名称(sorting键)
  3. 年龄

然后,我已经triyed更新年龄字段使用唯一ID(试图修改年龄30至40)这是我的代码

var AWS = require("aws-sdk"); AWS.config.update({ region: "us-east-1", }); var params = { TableName: 'test', Key: { id: '100' }, UpdateExpression: 'set #age = :age ', ConditionExpression: '#age = :testAge', ExpressionAttributeNames: { '#age': 'age' }, ExpressionAttributeValues: { ':age': '40', ':testAge': '30' } }; var docClient = new AWS.DynamoDB.DocumentClient(); docClient.update(params, function (err, data) { if (err) { console.log(err); } else { console.log(data); } }); 

但是我得到这样的错误。

 { [ValidationException: The provided key element does not match the schema] message: 'The provided key element does not match the schema', code: 'ValidationException', time: Thu Nov 17 2016 22:38:01 GMT+0530 (IST), requestId: '34PNMFM6CEACQIRHTSV77OI0JRVV4KQNSO5AEMVJF66Q9ASUAAJG', statusCode: 400, retryable: false, retryDelay: 0 } 

得到错误之后,我修改了像这样的paramsvariables

  var params = { TableName: 'test', Key: { id: '100',name: 'manaf' }, UpdateExpression: 'set #age = :age ', ConditionExpression: '#age = :testAge', ExpressionAttributeNames: { '#age': 'age' }, ExpressionAttributeValues: { ':age': '40', ':testAge': '30' } }; 

使用这个,更新成功完成。 如何更新表使用没有sorting键?

目前,DynamoDB更新API不具有仅通过分区键更新项目的选项。 没有与batchWriteItem类似的batchUpdateItem API。

因此,如果sorting键不可用,则获取分区键的所有sorting键,并更新分区和sorting键组合的每个项目。

对于主键,您必须提供所有的属性。 例如,对于一个简单的主键,您只需要为分区键提供一个值。 对于组合主键,您必须为分区键和sorting键提供值。

示例代码: –

您可能需要为您的表格进行更改。 下面的代码使用具有“年份”作为分区键和“标题”作为sorting键的“电影”表。

下面的代码更新给定散列键“2012”的“createdate”属性。

variablesparamsUpdate是基于查询操作形成的。 请根据您的要求(即表格结构)进行相应的更新。 逻辑保持不变,您只需要相应地更改表名和键值。

 var AWS = require("aws-sdk"); var creds = new AWS.Credentials('akid', 'secret', 'session'); AWS.config.update({ region : "us-west-2", endpoint : "http://localhost:8000", credentials : creds }); var docClient = new AWS.DynamoDB.DocumentClient(); var hashKey = 2012; var paramsQuery = { TableName : "Movies", KeyConditionExpression : 'yearkey = :hkey', ExpressionAttributeValues : { ':hkey' : hashKey } }; function updateItem(paramsUpdate) { console.log("Updating the item..."); docClient.update(paramsUpdate, function(err, data) { if (err) { console.error("Unable to update item. Error JSON:", JSON.stringify( err, null, 2)); } else { console.log("UpdateItem succeeded:", JSON.stringify(data)); } }); } docClient.query(paramsQuery, function(err, data) { if (err) { console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2)); } else { console.log(data.Count); var itemIndex = 0; while (itemIndex < data.Count) { console.log('Hashkey to be updated ======>', data.Items[itemIndex].yearkey, ';Title to be updated ========>', data.Items[itemIndex].title); var paramsUpdate = { TableName : "Movies", Key : { "yearkey" : data.Items[itemIndex].yearkey, "title" : data.Items[itemIndex].title }, UpdateExpression : "set #createdate = :createdate", ExpressionAttributeNames : { '#createdate' : 'createdate' }, ExpressionAttributeValues : { ':createdate' : '2016-11-17' }, ReturnValues : 'UPDATED_NEW' }; updateItem(paramsUpdate); itemIndex++; } } }); 

在DynamoDB中,分区键+sorting键被视为唯一标识项的“组合主键”(相反,Dynamo也支持只包含分区键的简单主键)。 所以你需要同时更新一个项目。 这是您可以拥有两个具有相同分区键但sorting键不同的原因。 因此,如果您只提供分区键,则Dynamo会对要更新的项目感到困惑。

对于当前的表configuration,更新给定分区键的项目的唯一方法是仅使用分区键进行查询以获取所有项目,并使用预期的sorting键过滤出该项目。 然后使用分区键和sorting键的组合来更新这个项目。