且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果该项目不存在,如何防止在UpdateItem中创建新项目

更新时间:2023-12-02 18:20:28

您可以使用 ConditionExpression 。如果键(即userId)存在并且update属性(即isActive)不等于与您要更新的新值,则将进行更新。

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

编辑:-



这应该可行。应该是 ConditionExpression (不是ConditionalExpression)。

  var参数= {
TableName:'users',
键:{
'userId':'123'
},
UpdateExpression:'SET isActive =:isActiveVal',
ConditionExpression:'userId =:userIdVal和isActive< :isActiveVal',
ExpressionAttributeValues:{
':userIdVal':'123',
':isActiveVal':'false'
},
ReturnValues: ALL_NEW
};

更新,2020年:



您可以使用 ConditionExpression 中的 attribute_exists 来检查该项目是否存在。 (来源: 1 2

  const params = {
TableName:'users',
键:{
'userId':'123'
},
UpdateExpression:'SET isActive =:isActiveVal',
ConditionExpression:'attribute_exists(userId)',
ExpressionAttributeValues:{
':isActiveVal':'false'
},
ReturnValues: 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"
};