如何检查不在 Amazon DynamoDB 集合中的元素?
How to check for element not in a set in Amazon DynamoDB?
我正在尝试在 Amazon DynamoDB 中执行更新,但前提是 StringSet 不包含特定字符串。查看 AWS docs 发现存在 contains()
函数,可以在 ConditionExpression
内部使用。然后我尝试了这段代码:
function addLecture(event){
const params = {
TableName: 'lectures',
Key: {
'lecture_id': Number.parseInt(event.lecture_id)
},
UpdateExpression: 'SET numeric_attr = numeric_attr - :val ADD students :student',
ConditionExpression: '(numeric_attr > :limit) AND (NOT contains(students, :student))',
ExpressionAttributeValues: {
':val': 1,
':limit' : 0,
':student' : ddb.createSet(event.student)
},
ReturnValues : 'UPDATED_NEW'
}
return ddb.update(params).promise();
}
无论如何,如果我尝试执行更新,它实际上会执行,即使 String 已经在 StringSet 中。
如何检查是否缺少字符串?
我认为你的条件格式不正确:
:student
解析为 ddb.createSet(event.student)
。您应该将字符串变量传递给 contains
,而不是字符串集。从您指出的文档中:
The operand must be a String if the attribute specified by path is a String. If the attribute specified by path is a Set, the operand must be the set's element type.
我正在尝试在 Amazon DynamoDB 中执行更新,但前提是 StringSet 不包含特定字符串。查看 AWS docs 发现存在 contains()
函数,可以在 ConditionExpression
内部使用。然后我尝试了这段代码:
function addLecture(event){
const params = {
TableName: 'lectures',
Key: {
'lecture_id': Number.parseInt(event.lecture_id)
},
UpdateExpression: 'SET numeric_attr = numeric_attr - :val ADD students :student',
ConditionExpression: '(numeric_attr > :limit) AND (NOT contains(students, :student))',
ExpressionAttributeValues: {
':val': 1,
':limit' : 0,
':student' : ddb.createSet(event.student)
},
ReturnValues : 'UPDATED_NEW'
}
return ddb.update(params).promise();
}
无论如何,如果我尝试执行更新,它实际上会执行,即使 String 已经在 StringSet 中。
如何检查是否缺少字符串?
我认为你的条件格式不正确:
:student
解析为 ddb.createSet(event.student)
。您应该将字符串变量传递给 contains
,而不是字符串集。从您指出的文档中:
The operand must be a String if the attribute specified by path is a String. If the attribute specified by path is a Set, the operand must be the set's element type.