如果项目不存在,如何防止在 UpdateItem 中创建新项目 [英] How to prevent creating a new item in UpdateItem if the item does not exist

查看:14
本文介绍了如果项目不存在,如何防止在 UpdateItem 中创建新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个用 Node.js 编写的 AWS Lambda 服务,它与 DynamoDB 数据库交互.我的一种方法在 DynamoDB 上执行更新 (AWS.DynamoDB.DocumentClient().update) 以更新特定项目.但是,我的问题是,当我尝试更新一个项目并且该项目不存在时,更新方法会将这个新项目添加到我的表中.此行为也是 文档 中描述的默认行为.p>

如果新项目尚不存在,我不希望我的方法创建它.我希望只有在记录存在时才进行更新.如果它不存在,它必须什么都不做.我如何实现这一目标?我希望我应该能够使用 ConditionalExpression 来实现这一点,但我在这方面所做的尝试并没有成功.

下面给出了我发送到更新函数的当前参数示例.在此示例中,我想更新具有 userId 123 的用户,并且我想将此用户的 isActive 字段设置为false"(但我只想在用户 123 实际存在的情况下这样做!)

常量参数 = {表名:'用户',钥匙: {用户ID:'123',},表达式属性值:{':isActive': '假'},UpdateExpression: 'SET isActive = :isActive',返回值:'ALL_NEW',};

解决方案

您可以为此使用 ConditionExpression.如果密钥(即 userId)存在且更新属性(即 isActive)不等于您尝试更新的新值,则会发生更新.

ConditionExpression: "userId = :userIdVal and isActive <> :isActiveVal",表达式属性值:{':isActive': '假',':userIdVal': '123'},

-

这应该可行.它应该是 ConditionExpression(不是 ConditionalExpression).

var params = {表名:'用户',钥匙: {用户 ID":123"},UpdateExpression: 'SET isActive = :isActiveVal',ConditionExpression: 'userId = :userIdVal and isActive <>:isActiveVal',表达式属性值:{':userIdVal': '123',':isActiveVal': '假'},返回值:ALL_NEW"};

2020 年更新:

您可以在ConditionExpression中使用attribute_exists来检查项目是否存在.(来源:12)

const params = {表名:'用户',钥匙: {用户 ID":123"},UpdateExpression: 'SET isActive = :isActiveVal',ConditionExpression: 'attribute_exists(userId)',表达式属性值:{':isActiveVal': '假'},返回值:ALL_NEW"};

I am running an AWS Lambda service written in Node.js that interacts with a DynamoDB database. One of my methods performs an update (AWS.DynamoDB.DocumentClient().update) on DynamoDB to update a specific item. My problem is, however, that when I try to update an item and that item does not exist, then the update method adds this new item to my table. This behavior is the default described in the documentation too.

I do not want my method to create a new item if it does not yet exist. I want this to be an update only if the record exists. If it doesn't exist it must do nothing. How do I achieve this? I expect I should be able to use a ConditionalExpression to achieve this, but the attempts I've made at doing this have not been successful.

An example of current parameters I'm sending to the update function is given below. In this example I want to update the user that has userId 123 and I want to set the isActive field to 'false' for this user (but I only want to do this if user 123 actually exists!)

const params = {
    TableName: 'users',
    Key: {
        userId: '123',
    },
    ExpressionAttributeValues: {
        ':isActive': 'false'
    },
    UpdateExpression: 'SET isActive = :isActive',
    ReturnValues: 'ALL_NEW',
};

解决方案

You can use ConditionExpression for this. The update will happen if the key (i.e. userId) is present and update attribute (i.e. isActive) is not equal to the new value that you are trying to update.

ConditionExpression: "userId = :userIdVal and isActive <> :isActiveVal",
ExpressionAttributeValues: {
    ':isActive': 'false',
    ':userIdVal': '123'
},

EDIT:-

This should work. It should be ConditionExpression (not ConditionalExpression).

var params = {
    TableName: 'users',
    Key: {
        'userId': '123'
    },
    UpdateExpression: 'SET isActive = :isActiveVal',
    ConditionExpression: 'userId = :userIdVal and isActive <> :isActiveVal',
    ExpressionAttributeValues: {
        ':userIdVal': '123',
        ':isActiveVal': 'false'
    },
    ReturnValues: "ALL_NEW"
};

Update, 2020:

You can use attribute_exists in ConditionExpression to check if the item exists or not. (Sources: 1, 2)

const params = {
    TableName: 'users',
    Key: {
        'userId': '123'
    },
    UpdateExpression: 'SET isActive = :isActiveVal',
    ConditionExpression: 'attribute_exists(userId)',
    ExpressionAttributeValues: {
        ':isActiveVal': 'false'
    },
    ReturnValues: "ALL_NEW"
};

这篇关于如果项目不存在,如何防止在 UpdateItem 中创建新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆