我是DynamoDB的新手,我试图设计一个表并做一些概念证明。下面是我使用“本地二级索引”创建的表
var params = {
TableName : "sample",
KeySchema: [
{ AttributeName: "user_id", KeyType: "HASH"},
{ AttributeName: "created_date", KeyType: "RANGE"}
],
AttributeDefinitions: [
{ AttributeName: "user_id", AttributeType: "S" },
{ AttributeName: "created_date", AttributeType: "S" },
{ AttributeName: "video_id", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
},
LocalSecondaryIndexes: [{
IndexName: "user_id-video_id-Index",
KeySchema: [
{
AttributeName: "user_id",
KeyType: "HASH"
},
{
AttributeName: "video_id",
KeyType: "RANGE"
}
],
Projection: {
ProjectionType: "ALL"
}
}]
然后,我在下面插入了两个项目
项目1:
{
"company": "comapnyname",
"created_date": "02-28-2019",
"email": "randomguy@abc.com",
"first_name": "John",
"last_name": "Doe",
"profile_pic": "https://s3location",
"user_id": "vshdhfb"
}
项目2:
{
"author": "Phil DiMaggio",
"created_date": "02-29-2018",
"description": "This video further introduces the Business Sales compensation plans.",
"likes": "12",
"title": "Understanding Business Sales Compensation Plans",
"user_id": "vshdhfb",
"video_id": "vdhk23so9",
"video_loc": "https://s3.amazonaws.com/videos/video.mp4",
"views": "45"
}
然后,我尝试使用以下代码更新/删除第二项,它给我一个错误,显示为“消息”:“所需键之一未提供值”
var params = {
TableName: "sample",
Key:{
"video_id": "vdhk23so9",
"user_id":"vshdhfb"
},
UpdateExpression: "set likes = likes + :val",
ExpressionAttributeValues:{
":val":1
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, function(err, data) {
if (err) {
document.getElementById('textarea').innerHTML = "Unable to update like: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('textarea').innerHTML = "Like video succeeded: " + "\n" + JSON.stringify(data, undefined, 2);
}
});
}
我需要添加另一个密钥吗?我想念什么?
答案 0 :(得分:3)
您的第三个代码块未提供create_date
的值。此字段定义为键的一部分:
TableName : "sample",
KeySchema: [
{ AttributeName: "user_id", KeyType: "HASH"},
{ AttributeName: "created_date", KeyType: "RANGE"}
您必须同时提供user_id
和created_date
才能唯一标识一条记录。
哦,可能不会引起问题,但这是无效的日期,因为2018年不是a年:
"created_date": "02-29-2018",
日期以字符串形式存储,因此不会引起错误,但是您也可以考虑以更有用的格式存储日期,例如YYYY-MM-DD
,这样可以更轻松地进行排序并避免与美国日期格式(例如01-02-2019
是2月1日还是1月2日?)。