2

这是我将项目插入数据库的方式:

DynamoDB.putItemAsync({
    "TableName": tblName,
    "Item": {
        "UserId": { "S": String(obj.user_id) },
        "CampaignId": { "N": String(obj.campaign_id) },
        "Email": { "S": String(obj.email) },
        "CustomActivityNodeId": { "N": String(obj.custom_activity_node_id) }
    },
    "Expected": {
        "UserId": {
            "Exists": false
        }
    }
})

putItemAsync打电话是因为我的promisified图书馆有bluebird. 我试过这样做:

{
    "Expected": {
        "UserId": {
            "Exists": false
        }
    }
}

我的putItem电话,但我没有运气。我要做的就是插入记录而不更新现有记录

4

1 回答 1

3

来自PutItem API 参考

要防止新项目替换现有项目,请使用包含 attribute_not_exists 函数的条件表达式,并将属性名称用作表的分区键。由于每条记录都必须包含该属性,因此仅当不存在匹配项时,attribute_not_exists 函数才会成功。

首先,确保UserId是表的 PK,然后您可以使用ConditionExpressionwith attribute_not_exists

DynamoDB.table('users').PutItem({}, {
    ConditionExpression: "attribute_not_exists(email)"
}).then((response) => response.json()).then((res) => {
    // [LOG] possible bug with time
})

当电子邮件(表中的我的 pk)以前不存在时,才会创建一个项目。

PS:我在 react-native 中使用了 dynamodb的包装器。

于 2016-07-15T20:21:56.163 回答