update_item
async
¶
update_item(
table_name: TableArn,
key: Key,
*,
config_overrides: Optional[AsyncDynamoDBClientConfig] = None,
attribute_updates: Optional[AttributeUpdates] = None,
expected: Optional[ExpectedAttributeMap] = None,
conditional_operator: Optional[ConditionalOperator] = None,
return_values: Optional[ReturnValue] = None,
return_consumed_capacity: Optional[ReturnConsumedCapacity] = None,
return_item_collection_metrics: Optional[ReturnItemCollectionMetrics] = None,
update_expression: Optional[UpdateExpression] = None,
condition_expression: Optional[ConditionExpression] = None,
expression_attribute_names: Optional[ExpressionAttributeNameMap] = None,
expression_attribute_values: Optional[ExpressionAttributeValueMap] = None,
return_values_on_condition_check_failure: Optional[
ReturnValuesOnConditionCheckFailure
] = None,
) -> UpdateItemOutput
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.
Parameters:
-
table_name(TableArn) –The name of the table containing the item to update. You can also provide the Amazon Resource Name (ARN) of the table in this parameter.
-
key(Key) –The primary key of the item to be updated. Each element consists of an attribute name and a value for that attribute.
For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.
-
attribute_updates(Optional[AttributeUpdates], default:None) –This is a legacy parameter. Use
UpdateExpressioninstead. For more information, see AttributeUpdates in the Amazon DynamoDB Developer Guide. -
expected(Optional[ExpectedAttributeMap], default:None) –This is a legacy parameter. Use
ConditionExpressioninstead. For more information, see Expected in the Amazon DynamoDB Developer Guide. -
conditional_operator(Optional[ConditionalOperator], default:None) –This is a legacy parameter. Use
ConditionExpressioninstead. For more information, see ConditionalOperator in the Amazon DynamoDB Developer Guide. -
return_values(Optional[ReturnValue], default:None) –Use
ReturnValuesif you want to get the item attributes as they appear before or after they are successfully updated. ForUpdateItem, the valid values are:-
NONE- IfReturnValuesis not specified, or if its value isNONE, then nothing is returned. (This setting is the default forReturnValues.) -
ALL_OLD- Returns all of the attributes of the item, as they appeared before the UpdateItem operation. -
UPDATED_OLD- Returns only the updated attributes, as they appeared before the UpdateItem operation. -
ALL_NEW- Returns all of the attributes of the item, as they appear after the UpdateItem operation. -
UPDATED_NEW- Returns only the updated attributes, as they appear after the UpdateItem operation.
There is no additional cost associated with requesting a return value aside from the small network and processing overhead of receiving a larger response. No read capacity units are consumed.
The values returned are strongly consistent.
-
-
return_item_collection_metrics(Optional[ReturnItemCollectionMetrics], default:None) –Determines whether item collection metrics are returned. If set to
SIZE, the response includes statistics about item collections, if any, that were modified during the operation are returned in the response. If set toNONE(the default), no statistics are returned. -
update_expression(Optional[UpdateExpression], default:None) –An expression that defines one or more attributes to be updated, the action to be performed on them, and new values for them.
The following action values are available for
UpdateExpression.-
SET- Adds one or more attributes and values to an item. If any of these attributes already exist, they are replaced by the new values. You can also useSETto add or subtract from an attribute that is of type Number. For example:SET myNum = myNum + :valSETsupports the following functions:-
if_not_exists (path, operand)- if the item does not contain an attribute at the specified path, thenif_not_existsevaluates to operand; otherwise, it evaluates to path. You can use this function to avoid overwriting an attribute that may already be present in the item. -
list_append (operand, operand)- evaluates to a list with a new element added to it. You can append the new element to the start or the end of the list by reversing the order of the operands.
These function names are case-sensitive.
-
-
REMOVE- Removes one or more attributes from an item. -
ADD- Adds the specified value to the item, if the attribute does not already exist. If the attribute does exist, then the behavior ofADDdepends on the data type of the attribute:-
If the existing attribute is a number, and if
Valueis also a number, thenValueis mathematically added to the existing attribute. IfValueis a negative number, then it is subtracted from the existing attribute.If you use
ADDto increment or decrement a number value for an item that doesn't exist before the update, DynamoDB uses0as the initial value.Similarly, if you use
ADDfor an existing item to increment or decrement an attribute value that doesn't exist before the update, DynamoDB uses0as the initial value. For example, suppose that the item you want to update doesn't have an attribute nameditemcount, but you decide toADDthe number3to this attribute anyway. DynamoDB will create theitemcountattribute, set its initial value to0, and finally add3to it. The result will be a newitemcountattribute in the item, with a value of3. -
If the existing data type is a set and if
Valueis also a set, thenValueis added to the existing set. For example, if the attribute value is the set[1,2], and theADDaction specified[3], then the final attribute value is[1,2,3]. An error occurs if anADDaction is specified for a set attribute and the attribute type specified does not match the existing set type.Both sets must have the same primitive data type. For example, if the existing data type is a set of strings, the
Valuemust also be a set of strings.
The
ADDaction only supports Number and set data types. -
-
DELETE- Deletes an element from a set.If a set of values is specified, then those values are subtracted from the old set. For example, if the attribute value was the set
[a,b,c]and theDELETEaction specifies[a,c], then the final attribute value is[b]. Specifying an empty set is an error.The
DELETEaction only supports set data types.
You can have many actions in a single expression, such as the following:
SET a=:value1, b=:value2 DELETE :value3, :value4, :value5For more information on update expressions, see Modifying Items and Attributes in the Amazon DynamoDB Developer Guide.
-
-
condition_expression(Optional[ConditionExpression], default:None) –A condition that must be satisfied in order for a conditional update to succeed.
An expression can contain any of the following:
-
Functions:
attribute_exists | attribute_not_exists | attribute_type | contains | begins_with | sizeThese function names are case-sensitive.
-
Comparison operators:
= | <> | < | > | <= | >= | BETWEEN | IN -
Logical operators:
AND | OR | NOT
For more information about condition expressions, see Specifying Conditions in the Amazon DynamoDB Developer Guide.
-
-
expression_attribute_names(Optional[ExpressionAttributeNameMap], default:None) –One or more substitution tokens for attribute names in an expression. The following are some use cases for using
ExpressionAttributeNames:-
To access an attribute whose name conflicts with a DynamoDB reserved word.
-
To create a placeholder for repeating occurrences of an attribute name in an expression.
-
To prevent special characters in an attribute name from being misinterpreted in an expression.
Use the # character in an expression to dereference an attribute name. For example, consider the following attribute name:
-
Percentile
The name of this attribute conflicts with a reserved word, so it cannot be used directly in an expression. (For the complete list of reserved words, see Reserved Words in the Amazon DynamoDB Developer Guide.) To work around this, you could specify the following for
ExpressionAttributeNames:-
{"#P":"Percentile"}
You could then use this substitution in an expression, as in this example:
-
#P = :val
Tokens that begin with the : character are expression attribute values, which are placeholders for the actual value at runtime.
For more information about expression attribute names, see Specifying Item Attributes in the Amazon DynamoDB Developer Guide.
-
-
expression_attribute_values(Optional[ExpressionAttributeValueMap], default:None) –One or more values that can be substituted in an expression.
Use the : (colon) character in an expression to dereference an attribute value. For example, suppose that you wanted to check whether the value of the
ProductStatusattribute was one of the following:Available | Backordered | DiscontinuedYou would first need to specify
ExpressionAttributeValuesas follows:{ ":avail":{"S":"Available"}, ":back":{"S":"Backordered"}, ":disc":{"S":"Discontinued"} }You could then use these values in an expression, such as this:
ProductStatus IN (:avail, :back, :disc)For more information on expression attribute values, see Condition Expressions in the Amazon DynamoDB Developer Guide.
-
return_values_on_condition_check_failure(Optional[ReturnValuesOnConditionCheckFailure], default:None) –An optional parameter that returns the item attributes for an
UpdateItemoperation that failed a condition check.There is no additional cost associated with requesting a return value aside from the small network and processing overhead of receiving a larger response. No read capacity units are consumed.
Raises:
-
ConditionalCheckFailedException–A condition specified in the operation failed to be evaluated.
-
InternalServerError–An error occurred on the server side.
-
ItemCollectionSizeLimitExceededException–An item collection is too large. This exception is only returned for tables that have one or more local secondary indexes.
-
ProvisionedThroughputExceededException–The request was denied due to request throttling. For detailed information about why the request was throttled and the ARN of the impacted resource, find the ThrottlingReason field in the returned exception. The Amazon Web Services 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.
-
ReplicatedWriteConflictException–The request was rejected because one or more items in the request are being modified by a request in another Region.
-
RequestLimitExceeded–Throughput exceeds the current throughput quota for your account. For detailed information about why the request was throttled and the ARN of the impacted resource, find the ThrottlingReason field in the returned exception. Contact Amazon Web Services Support to request a quota increase.
-
ResourceNotFoundException–The operation tried to access a nonexistent table or index. The resource might not be specified correctly, or its status might not be
ACTIVE. -
ThrottlingException–The request was denied due to request throttling. For detailed information about why the request was throttled and the ARN of the impacted resource, find the ThrottlingReason field in the returned exception.
-
TransactionConflictException–Operation was rejected because there is an ongoing transaction for the item.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To update an item in a table This example updates an item in the Music table. It adds a new attribute (Year) and modifies the AlbumTitle attribute. All of the attributes in the item, as they appear after the update, are returned in the response.
>>> await client.update_item(table_name='Music', key={'Artist': {'S': 'Acme Band'}, 'SongTitle': {'S': 'Happy Day'}}, update_expression='SET #Y = :y, #AT = :t', expression_attribute_names={'#Y': 'Year', '#AT': 'AlbumTitle'}, expression_attribute_values={':y': {'N': '2015'}, ':t': {'S': 'Louder Than Ever'}}, return_values='ALL_NEW')
Source code in docs_src/capo_dynamodb/_services/async_dynamo_db.py
4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 | |