UpdateItem() public method

Edits an existing item's attributes, or adds a new item to the table if it does not already exist. You can put, delete, or add attribute values. You can also perform a conditional update on an existing item (insert a new attribute name-value pair if it doesn't exist, or replace an existing name-value pair if it has certain expected attribute values).

You can also return the item's attribute values in the same UpdateItem operation using the ReturnValues parameter.

/// A condition specified in the operation could not be evaluated. /// /// An error occurred on the server side. /// /// An item collection is too large. This exception is only returned for tables that have /// one or more local secondary indexes. /// /// Your request rate is too high. The AWS SDKs for DynamoDB automatically retry requests /// that receive this exception. Your request is eventually successful, unless your retry /// queue is too large to finish. Reduce the frequency of requests and use exponential /// backoff. For more information, go to Error /// Retries and Exponential Backoff in the Amazon DynamoDB Developer Guide. /// /// The operation tried to access a nonexistent table or index. The resource might not /// be specified correctly, or its status might not be ACTIVE. ///
public UpdateItem ( UpdateItemRequest request ) : UpdateItemResponse
request Amazon.DynamoDBv2.Model.UpdateItemRequest Container for the necessary parameters to execute the UpdateItem service method.
return Amazon.DynamoDBv2.Model.UpdateItemResponse
        private static void ConditionalWriteSample(AmazonDynamoDBClient client)
        {
            Table table = Table.LoadTable(client, "Actors");
            Document d = table.GetItem("Christian Bale");

            int version = d["Version"].AsInt();
            double height = d["Height"].AsDouble();

            Console.WriteLine("Retrieved Item Version #" + version.ToString());

            var request = new UpdateItemRequest
            {
                Key = new Dictionary<string, AttributeValue>() { { "Name", new AttributeValue { S = "Christian Bale" } } },
                ExpressionAttributeNames = new Dictionary<string, string>()
                {
                    {"#H", "Height"},
                    {"#V", "Version"}
                },
                ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    {":ht", new AttributeValue{N=(height+.01).ToString()}},
                    {":incr", new AttributeValue{N="1"}},
                    {":v", new AttributeValue{N=version.ToString()}}
                },
                UpdateExpression = "SET #V = #V + :incr, #H = :ht",
                ConditionExpression = "#V = :v",
                TableName = "Actors"
            };

            try
            {
                Console.ReadKey();
                var response = client.UpdateItem(request);
                Console.WriteLine("Updated succeeded.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Update failed. " + ex.Message);
            }

            Console.ReadKey();
        }
Example #2
0
        public void CRUDSamples()
        {
            EnsureTables();

            PutSample();

            {
                #region GetItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Create GetItem request
                GetItemRequest request = new GetItemRequest
                {
                    TableName = "SampleTable",
                    Key = key,
                };

                // Issue request
                var result = client.GetItem(request);

                // View response
                Console.WriteLine("Item:");
                Dictionary<string, AttributeValue> item = result.Item;
                foreach (var keyValuePair in item)
                {
                    Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]",
                        keyValuePair.Key,
                        keyValuePair.Value.S,
                        keyValuePair.Value.N,
                        string.Join(", ", keyValuePair.Value.SS ?? new List<string>()),
                        string.Join(", ", keyValuePair.Value.NS ?? new List<string>()));
                }

                #endregion
            }

            {
                #region UpdateItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Define attribute updates
                Dictionary<string, AttributeValueUpdate> updates = new Dictionary<string, AttributeValueUpdate>();
                // Update item's Setting attribute
                updates["Setting"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.PUT,
                    Value = new AttributeValue { S = "St. Petersburg, Missouri" }
                };
                // Remove item's Bibliography attribute
                updates["Bibliography"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.DELETE
                };   
                // Add a new string to the item's Genres SS attribute
                updates["Genres"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.ADD,
                    Value = new AttributeValue { SS = new List<string> { "Bildungsroman" } }
                };

                // Create UpdateItem request
                UpdateItemRequest request = new UpdateItemRequest
                {
                    TableName = "SampleTable",
                    Key = key,
                    AttributeUpdates = updates
                };

                // Issue request
                client.UpdateItem(request);

                #endregion
            }

            {
                #region DeleteItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Create DeleteItem request
                DeleteItemRequest request = new DeleteItemRequest
                {
                    TableName = "SampleTable",
                    Key = key
                };

                // Issue request
                client.DeleteItem(request);

                #endregion
            }
        }
        public virtual void UpdateIfMatch(AmazonDynamoDBClient ddbClient, string tableName, string email, string company,
            string firstNameTarget, string firstNameMatch)
        {
            // Build the request
            var updateItemRequest = new UpdateItemRequest
            {
                TableName = tableName,
                Key = new Dictionary<string, AttributeValue>
                {
                    {"Company", new AttributeValue {S = company}},
                    {"Email", new AttributeValue {S = email}}
                },
                Expected = new Dictionary<string, ExpectedAttributeValue>
                {
                    {"First", new ExpectedAttributeValue {Value = new AttributeValue {S = firstNameMatch}}}
                },
                AttributeUpdates = new Dictionary<string, AttributeValueUpdate>
                {
                    {
                        "First",
                        new AttributeValueUpdate {Action = "PUT", Value = new AttributeValue {S = firstNameTarget}}
                    }
                }
            };

            // Submit request
            ddbClient.UpdateItem(updateItemRequest);
        }