Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add an empty or data map in dynamodb?

I have something like this:

{
  Records:{}
}

and I just want to add data inside records like this:

{
  Id: 123,
  Records:{
    10001: { event : "item1" }
  }
}

My 1st attempt:

var params = {
    TableName : "Records",
    Key : { Id : 123 },
    UpdateExpression: 'set Record.#k1.event = :v1',
    ExpressionAttributeNames: { '#k1' :  10001},
    ExpressionAttributeValues: { ':v1' : 'item1' }
};

Because Record.10001 doesn't exist, it give me error:

The document path provided in the update expression is invalid for update

"ADD" only support NUMBER and SET type so its not suitable in my case.

My 2nd attempt (trying to create a empty map 1st):

var params = {
    TableName : "Records",
    Key : { Id : 123 },
    UpdateExpression: 'set Record.#k1 = {}',
    ExpressionAttributeNames: { '#k1' :  10001},
};

It fails as my syntax is probably incorrect and I cannot find how to create a empty map to an attribute.

So my question is how do I add structured object as a map or empty map to an existing item like the example above?

I can easily modifying the data from the amazon DynamoDB data management page so at least I know there exist a way to do it. I just need to know how.

Thanks

like image 997
Nick Avatar asked Feb 23 '16 05:02

Nick


People also ask

Does DynamoDB accept empty string?

Empty value support gives you greater flexibility to use attributes for a broader set of use cases without having to transform such attributes before sending them to DynamoDB. List, Map, and Set data types also support empty String and Binary values.

Does DynamoDB support map?

SS (string set) type, NS (number set) type, or BS (binary set) type. The DynamoDBTypeConverter interface lets you map your own arbitrary data types to a data type that is natively supported by DynamoDB. For more information, see Mapping arbitrary data.


2 Answers

I somehow successfully done it this way:

var params = {
    TableName: "Records",
    Key: { Id: 123 },
    UpdateExpression: 'set Records.#k1 = :v1',
    ExpressionAttributeNames: {'#k1': '10001',},
    ExpressionAttributeValues: {':v1': { event: 'Yay~' } }
};

optionally you could use

    UpdateExpression: 'set Records.#k1 = if_not_exists( Records.#k1, :v1)',

to avoid overwriting existing records.

like image 192
Nick Avatar answered Sep 17 '22 18:09

Nick


I found a workaround for the error we get.

try:
    table.update_item( 
          Key = {'Id' : id},
          UpdateExpression = 'SET Records.#key1.#key2 = :value1',
          ExpressionAttributeNames = {'#key1': '10001', '#key2' : 'event'},
          ExpressionAttributeValues = {':value1': 'singing'}
         )
except:
    table.update_item( 
          Key = {'Id' : id},
          UpdateExpression = 'SET Records = :value1',
          ExpressionAttributeValues = {':value1': {'10001': {'event' : 'dance'}}}
          )

When we try to update for the first time we get "The document path provided in the update expression is invalid for update" error. An exception will be thrown since there will be no nested JSON.

Thereafter, any updates say for example Record.10002 will not throw exception. Update function in the try block will be executed.

Please comment on this if this is the correct way of handling this kind of scenario.

like image 36
manojpt Avatar answered Sep 19 '22 18:09

manojpt