node.js - 停止 dynamodb 中的重复条目

标签 node.js amazon-web-services amazon-dynamodb

我在 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.

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ConditionExpressions.html :

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/

相关文章:

javascript - Node.js Socket.io 错误 : disconnect event is triggered after connect

image - 使用 node.js 跨平台调整图像大小

amazon-web-services - 如何限制对特定前缀中的 S3 key 的访问? (在 Python/boto 中)

node.js - 来自 Lambda 的 AWSXRay.captureAsyncFunc() - 我错过了什么吗?

java - 使用 Java 高级 API 从 DynamoDB 表中获取所有表项

javascript - 正则表达式字符集 - 以及它们包含的内容

node.js - 缓冲区类型视频宽高nodejs

amazon-web-services - 读取 CloudFormation 用户数据中的 AWS secret

node.js - 为什么Amazon DynamoDB的Delete方法不返回删除的结果?

java - DynamoDB 中的 RateLimited 写入