How to prevent creating a new item in UpdateItem if the item does not exist

前端 未结 3 1592
鱼传尺愫
鱼传尺愫 2020-12-29 17:53

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

相关标签:
3条回答
  • 2020-12-29 18:37

    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"
    };
    
    0 讨论(0)
  • 2020-12-29 18:46

    I bumped into a somehow similar problem using attribute_exists, the only difference is, I need to query from the table and get only the record that does have the attribute I need. Using ConditionExpression did not work for me. Thought of sharing it here in case someone is in need.

    Here, if the subscriberKey is equal to given param and the attribute_exists (mobileNumber) within the record, it returns true, false otherwise.

    var params = {
        TableName : table_name,
        IndexName: "subsciberKey-index",
        KeyConditionExpression: "subscriberKey = :subscriberKey",
        FilterExpression: "attribute_exists(mobileNumber)",
        ExpressionAttributeValues: {
            ":subscriberKey": subscriberNo
        }
    };

    0 讨论(0)
  • 2020-12-29 18:49

    ConditionExpression is the way to go, but you can also use the attribute_exists function :

    ConditionExpression: 'attribute_exists(userId)'
    

    It will only update the line with the correct key and throw a ConditionalCheckFailedException if requested key doesn't exist.

    The goal is more obvious and you can skip the extra userId binding.

    ref : https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html

    0 讨论(0)
提交回复
热议问题