node.js - Cant update Dynamo Db table , getting ValidationException -
i need update dynamo db table using partition key. got validation exeption. have created table 3 fields.
- id (partition key)
- name (sort key)
- age
then have triyed update age field using id.(tryied modify age 30 40) code
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); } });
but got error this.
{ [validationexception: provided key element not match schema] message: 'the provided key element not match schema', code: 'validationexception', time: thu nov 17 2016 22:38:01 gmt+0530 (ist), requestid: '34pnmfm6ceacqirhtsv77oi0jrvv4kqnso5aemvjf66q9asuaajg', statuscode: 400, retryable: false, retrydelay: 0 }
after getting error, modified params variable this
var params = { tablename: 'test', key: { id: '100',name: 'manaf' }, updateexpression: 'set #age = :age ', conditionexpression: '#age = :testage', expressionattributenames: { '#age': 'age' }, expressionattributevalues: { ':age': '40', ':testage': '30' } };
using this, updation completed. how update table using without sort key?
currently, dynamodb update api doesn't have option update item partition key only. there no batchupdateitem api similar batchwriteitem well.
so, if sort key not available, sort keys of partition key , update each item partition , sort key combination.
for primary key, must provide of attributes. example, simple primary key, need provide value partition key. composite primary key, must provide values both partition key , sort key.
sample code:-
you may need change table. below code uses "movies" table has "yearkey" partition key , "title" sort key.
the below code updates "createdate" attribute given hash key "2012".
the variable paramsupdate formed based on query operation. please update accordingly requirement (i.e. table structure). logic remains same, need change table name , key values accordingly.
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 item..."); docclient.update(paramsupdate, function(err, data) { if (err) { console.error("unable 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 read item. error json:", json.stringify(err, null, 2)); } else { console.log(data.count); var itemindex = 0; while (itemindex < data.count) { console.log('hashkey updated ======>', data.items[itemindex].yearkey, ';title 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++; } } });
Comments
Post a Comment