update_item
¶
update_item(
table_name: TableArn,
key: Key,
*,
config_overrides: Optional[DynamoDBClientConfig] = 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.
>>> 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/dynamo_db.py
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 | |