Search code examples
phpamazon-web-servicesamazon-dynamodbaws-php-sdk

how to use if_not_exists() for a key of a map in Dynamodb's updateItem() in PHP API


thank you for your time first!

The map:

Country:{
  Sweden: 3,
  US: 4,
  UK: 9
}

How could I check if the country name exists in the map, if not then creating it and set it to a default value. For example, Spain is not in the map, how could I use if_not_exits()?

The document mentioned this function here :http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html .

I found someone applied the if_not_exits() for a list: Is it possible to combine if_not_exists and list_append in update_item. Then how it works for a map?

Looking forward to your reply and thank you so much!

Sincerely


Solution

  • I found the solution for using if_not_exits for a map:

    'ExpressionAttributeValues' => [
                ':val2' => ['N' => '9']
    
        ],
    
        'UpdateExpression' => 'SET Country.Spain = if_not_exists(Country.Spain, :val2)'
    

    In addition, I also found the solution to check first and make change if it do exist:

    'ExpressionAttributeValues' => [
                    ':val2' => ['N' => '9'],
                    ':val3' => ['N' => '1']
    
            ],
    
            'UpdateExpression' => 'SET country_name.Sweden = if_not_exists(country_name.Sweden, :val2) + :val3 '