我在 Dynamodb 中有一个名为 followProduct 的表,它具有以下结构
id - item id
email - user email
product - product id
每当用户关注某个产品时,我都会在表中进行记录。我正在尝试停止重复输入并使用以下代码
let params = {
TableName: "followProduct",
ConditionExpression: "email <> :email AND product <> :pid",
Item: {
email: "a@a.com",
product: req.body.productId,
id: shortid.generate()
},
ExpressionAttributeValues: {
':email': "a@a.com",
":pid": req.body.productId
}
};
createItemInDDB(params).then(() => {
res.status(200).send("Company Added");
}, err => {
console.log(err);
res.sendStatus(500);
});
CreateItemInDDB 只是一个以参数作为输入并运行文档客户端提供的 put 函数的函数。该参数仍然会产生重复的条目。我希望对于每封电子邮件,每个产品 ID 只能输入一次。
最佳答案
您能描述一下您的表哈希范围键吗?
Dynamodb 只能强制哈希范围表键的唯一性(不适用于全局二级索引键)
来自http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html
To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the partition key for the table. Since every record must contain that attribute, the attribute_not_exists function will only succeed if no matching item exists.
The PutItem operation will overwrite an item with the same key (if it exists). If you want to avoid this, use a condition expression. This will allows the write to proceed only if the item in question does not already have the same key:
关于node.js - 停止 dynamodb 中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47446765/