关于亚马逊网络服务:Dynamodb 批量放置如果项目不存在

Dynamodb batch put if item does not exist

DynamoDb 不支持批量更新,只支持批量放置。

但是是否可以只批量放置键不存在的项目?


batchWriteItem中,有如下注释:

For example, you cannot specify conditions on individual put and delete requests, and BatchWriteItem does not return deleted items in the response.

相反,我建议将 putItem 与条件表达式一起使用。 putItem 文档的底部有以下注释:

[...] 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 [...]

所以请确保将以下内容添加到 ConditionExpression(此处使用 NodeJS 语法)

1
2
3
4
5
6
7
8
const params = {
    Item: {
        userId: {
            S:"Beyonce",
        }
    },
    ConditionExpression:"attribute_not_exists(userId)"
};