Search code examples
amazon-dynamodboperation

Does DynamoDB support update operations like relational db


I know we can do update by two operations, first get the primary key by querying the db, and then update it by put operation. But does DynamoDB support update by one operation as the relational db (such as mysql)? Since two operations will cost more time in network transferring.

My situation is as: I have a table A with fields ID, Name, Location, Value.

And name+location can uniquely define a row. So now I want to update the field "Value" when Name and Location satisfied some condition, but I don't know the ID. So if I use mysql, then I can update it by "Update A set value = XXX where name = "abc" and location="123"". But when I use dynamoDB, I have to first get the primary key ID. Then use the Key to update the item. So my question is that does DynamoDB also support similar update operation as mysql does. Thanks!


Solution

  • Chen hit it on the nose. Joey, the situation you described (Get followed by a Put) is equivalent to 2 mysql functions

       SELECT * 
       FROM TABLE
       WHERE key = x
    
       UPDATE TABLE 
       SET var = param
       WHERE key = x
    

    Do you see how the Select/PutItem aren't part of the update process? As long as you have the keys, you don't need to perform a query. I'm assuming you're performing the GetItem before the PutItem request because the PutItem replaces the entire item/row (i.e. deletes all attributes not specified in the Put request).

    So if the original item looked like: < key-id=1, first-name=John, last-name=Doe, age=22>

    and you perform a PutItem of: < key-id=1,location=NY>

    The final item looks like: < key-id=1,location=NY>

    If you perform an UpdateItem in place of PutItem then you would instead get:

    < key-id=1, first-name=John, last-name=Doe, age=22, location=NY>

    Here's a link for using the UpdateItem with Java. There also examples using .net and php

    UpdateItem for Java