简体   繁体   English

如何将主键值传递给 DynamoDB?

[英]How to pass primary key value to DynamoDB?

I have a nodejs app and I am using putItem() to insert into DynamoDB.我有一个 nodejs 应用程序,我正在使用 putItem() 插入 DynamoDB。 The table that I am passing the value to has a primary key called "id".我将值传递给的表有一个名为“id”的主键。 Now what I am trying to do is to generate a UUID and set the primary key value to that UUID whenever I insert the data.现在我要做的是生成一个 UUID 并在我插入数据时将主键值设置为该 UUID。 However when I insert, the "id" is inserted with double quotes.但是,当我插入时,“id”会用双引号插入。 For eg: instead of id: 229f0a06-7257-4727-b825-a6e8ea343b1e it will show as id: "229f0a06-7257-4727-b825-a6e8ea343b1e" .例如:而不是id: 229f0a06-7257-4727-b825-a6e8ea343b1e它将显示为id: "229f0a06-7257-4727-b825-a6e8ea343b1e" I checked the schema and it shows that "id" KeyType is HASH .我检查了架构,它显示“id” KeyType 是HASH This is the code I have to insert into DB:这是我必须插入数据库的代码:

let data = {};
 data.id= uuidv4(); //Trying to set the primary key "id"
 data.name = "Test";
 
 const dynamoDB = new DynamoDBClient;
 const result = await dynamoDB.putItem(data);

When I go check in the DB table, it shows id: "229f0a06-7257-4727-b825-a6e8ea343b1e" .当我 go 检查数据库表时,它显示id: "229f0a06-7257-4727-b825-a6e8ea343b1e" How can I set the "id" so that it doesn't show up with double-quotes?如何设置“id”以使其不显示双引号?

Thanks.谢谢。

How can I set the "id" so that it doesn't show up with double-quotes?如何设置“id”以使其不显示双引号?

You don't want to.你不想。 Your Partition Key (= Hash Key) data type is String , which of course is the right data type for UUIDs.您的分区键(= Hash 键)数据类型是String ,这当然是 UUID 的正确数据类型。

Hash Key is a DynamoDB synonym for Partition Key . Hash KeyPartition Key的 DynamoDB 同义词。 As such, HASH in the CreateTable KeySchema API is not a data type, but rather is a reference to the Partition Key field.因此, CreateTable KeySchema API 中的 HASH HASH数据类型,而是对Partition Key字段的引用。 The docs continue, "the attributes in KeySchema must also be defined in the AttributeDefinitions" , to a data type to one of (S)tring , (N)umber or (B)inary .文档继续, “KeySchema 中的属性也必须在 AttributeDefinitions 中定义” ,数据类型为(S)tring(N)umber number 或(B)inary inary 之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM