将字符串添加到 dynamodb 中的列表 table
Add a string to a list in a dynamodb table
所以我一直在 nodejs express 应用程序中使用 dynamodb,我有一个特定的 table,它有一个只是空列表的字段,我想将一个字符串附加到列表中。
table 名称是 "dev_entrants",这里是 table 的示例:
----------------------------------------
primary Key Sort Key
eventID | eventType | entrants
----------------------------------------
Qual-919-w5wm1xhnw | Qual | []
----------------------------------------
所以我收到一个 post 请求,然后通过 express 路由它,它进入一个函数,在进行类型检查和所有这些之后,我尝试向我的 table 添加内容:
import AWS from 'aws-sdk';
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-1'});
const db = {
docClient
};
...
const entrantsParams = {
'TableName' : 'dev_entrants',
'Key': {
'eventID' : 'Qual-919-w5wm1xhnw',
},
'UpdateExpression' : "SET #attrName = list_append(#attrName, :attrValue)",
'ExpressionAttributeNames' : {
'#attrName' : 'entrants'
},
'ExpressionAttributeValues' : {
':attrValue' : ['joe'],
}
};
const updateEntrantsPromise = db.docClient.update(entrantsParams).promise();
(为了这个例子的目的,我用它们代表的字符串替换了变量)
我花了 6 个小时左右阅读不同的文档,并在堆栈溢出上试图找到答案。
我得到的当前错误是提供的关键元素与架构不匹配。如果我删除 attrValue 周围的括号,则会得到错误的操作数类型。我知道密钥存在于 table 中,因为我从那里复制并粘贴了它。此外,我还成功地从另一个函数向 table 添加内容,因此我的连接工作正常。谁能帮帮我?
您需要在 Key
对象中包含 eventType
,因为您的 table 架构有一个排序键。如果您的 table 有一个 sort/partition 键,那么您需要将它与主键一起包括在内。尝试以下方法:
const entrantsParams = {
'TableName' : 'dev_entrants',
'Key': {
'eventID' : 'Qual-919-w5wm1xhnw',
'eventType' : 'Qual'
},
'UpdateExpression' : "SET #attrName = list_append(if_not_exists(#attrName, :empty_list), :attrValue)",
'ExpressionAttributeNames' : {
'#attrName' : 'entrants'
},
'ExpressionAttributeValues' : {
':attrValue' : ['joe'],
':empty_list': []
}
};
所以我一直在 nodejs express 应用程序中使用 dynamodb,我有一个特定的 table,它有一个只是空列表的字段,我想将一个字符串附加到列表中。
table 名称是 "dev_entrants",这里是 table 的示例:
----------------------------------------
primary Key Sort Key
eventID | eventType | entrants
----------------------------------------
Qual-919-w5wm1xhnw | Qual | []
----------------------------------------
所以我收到一个 post 请求,然后通过 express 路由它,它进入一个函数,在进行类型检查和所有这些之后,我尝试向我的 table 添加内容:
import AWS from 'aws-sdk';
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-1'});
const db = {
docClient
};
...
const entrantsParams = {
'TableName' : 'dev_entrants',
'Key': {
'eventID' : 'Qual-919-w5wm1xhnw',
},
'UpdateExpression' : "SET #attrName = list_append(#attrName, :attrValue)",
'ExpressionAttributeNames' : {
'#attrName' : 'entrants'
},
'ExpressionAttributeValues' : {
':attrValue' : ['joe'],
}
};
const updateEntrantsPromise = db.docClient.update(entrantsParams).promise();
(为了这个例子的目的,我用它们代表的字符串替换了变量)
我花了 6 个小时左右阅读不同的文档,并在堆栈溢出上试图找到答案。
我得到的当前错误是提供的关键元素与架构不匹配。如果我删除 attrValue 周围的括号,则会得到错误的操作数类型。我知道密钥存在于 table 中,因为我从那里复制并粘贴了它。此外,我还成功地从另一个函数向 table 添加内容,因此我的连接工作正常。谁能帮帮我?
您需要在 Key
对象中包含 eventType
,因为您的 table 架构有一个排序键。如果您的 table 有一个 sort/partition 键,那么您需要将它与主键一起包括在内。尝试以下方法:
const entrantsParams = {
'TableName' : 'dev_entrants',
'Key': {
'eventID' : 'Qual-919-w5wm1xhnw',
'eventType' : 'Qual'
},
'UpdateExpression' : "SET #attrName = list_append(if_not_exists(#attrName, :empty_list), :attrValue)",
'ExpressionAttributeNames' : {
'#attrName' : 'entrants'
},
'ExpressionAttributeValues' : {
':attrValue' : ['joe'],
':empty_list': []
}
};