node.js - Pass Variable as Key Name in DynamoDB putItem JSON array -


i have simple json array trying set map key on using variable. array uses map functionality of dynamodb , first map called 'hours' contains nested map want have key of 15. becuase want key change depending on hour of day, passed variable json array nested map key reflect that.

for following hard coded 15 simplify problem.

the issue dynamodb in fact run putitem in there changes variable hour string "hour" , ignores value of set variable during operation. ideas on how pass variable value key name?

var hour = "15";  "hours" : {"m" : {     hour : {"m" : { //the hour variable used key           "action1" : {"n" : "1"},           "action2" : {"n" : "1"}            }      }   } } 

use expressionattributenames: , expressionattributevalues: in params. following (using documentclient convert javascript types automatically) should point in right direction have decide if you're adding new #hr nestedfields, deleting old ones, etc and, also, works if #hrs map exists begin in item--even if empty map. finally, overwrite existing #hr nestedfields if exists or create new ones if don't. again, not sure of absolute need here.

var hour = 15; //or whatever number need var dynamodbdc = new aws.dynamodb.documentclient(); var params = {         tablename:                 <yourtablename>,         key:                       <yourkey>,         updateexpression:          "set #hrs.#hr = :value",         expressionattributenames:  {"#hrs": "hours", "#hr": hour.tostring()},         expressionattributevalues: {":value": {"action1": 1, "action2": 2}},         returnvalues:              "all_new" //or whatever want here     }; dynamodbdc.update(params, function(err, data) {     if(err) {         //handle     }     else {         //your subsequent code     } } 

more info can found here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/expressionplaceholders.html#expressionattributenames

and here: http://docs.aws.amazon.com/awsjavascriptsdk/latest/aws/dynamodb/documentclient.html

i hope helps.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -