3

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

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Spider
  • 1,380
  • 4
  • 22
  • 42

2 Answers2

3

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 '
Morgoth
  • 4,935
  • 8
  • 40
  • 66
Spider
  • 1,380
  • 4
  • 22
  • 42
-2
<?php
$Country= array("Sweden"=>3, "US"=>4, "UK"=>9);
if(!array_key_exists("CN", $array))
{
    $Country['CN']=3;
}
?>
Yonghui
  • 37
  • 5
  • Thanks a lot for your answer! But my problem is not a pure php problem, it is happened in Amazon-dynamodb's PHP SDK. – Spider May 25 '16 at 09:18