I have the following table in dynamo:
id | comment_ids | updated_at
1 [12, 123] '2017-09-09'
when new comment has arrived (like 11
) how to update the corresponding id
to get:
id | comment_ids | updated_at
1 [12, 123, 11] '2017-09-09'
I'm not a Python developer, but the API you want to use is UpdateItem.
If the data type of the attribute "comment_ids" is a Set:
{
"TableName": "XXX",
"Key": {
"id": {
"N": "1"
}
},
"UpdateExpression": "ADD #cid :new_cid",
"ExpressionAttributeNames": {
"#cid": "comment_ids"
},
"ExpressionAttributeValues": {
":new_cid": {"N": "11" }
}
}
If the data type of the attribute "comment_ids" is a List:
{
"TableName": "XXX",
"Key": {
"id": {
"N": "1"
}
},
"UpdateExpression": "SET #cid = list_append(#cid, :new_cid)",
"ExpressionAttributeNames": {
"#cid": "comment_ids"
},
"ExpressionAttributeValues": {
":new_cid": {"N": "11" }
}
}
Keep in mind, difference between Set and Lists docs :
Sets Sets can only be of one Scalar type Empty sets are not supported Ordering within the set is not preserved Each value must be unique
Lists A list type attribute can store an ordered collection of values. Lists are enclosed in square brackets: [ ... ]
A list is similar to a JSON array. There are no restrictions on the data types that can be stored in a list element, and the elements in a list element do not have to be of the same type.
Use the operator or function accordingly to append to list or set.
list_append
- if the data type is LIST (ie L) Appending to List:-
1) Define the list and use it in ExpressionAttributeValues
(ie idVals)
idVal = [11]
response = table.update_item(
Key={
'#id': 1992
},
UpdateExpression="SET comment_ids = list_append(comment_ids , :idVal)",
ExpressionAttributeValues={
':idVal': idVal
},
ExpressionAttributeNames={
'#id' : 'id'
},
)
Appending to Set:-
1) Define the set and use it in ExpressionAttributeValues
(ie idVals)
idVals = set([11])
response = table.update_item(
Key={
'#id': 1999
},
UpdateExpression="ADD comment_ids :idVal",
ExpressionAttributeValues={
':idVal': idVals
},
ExpressionAttributeNames={
'#id' : 'id'
},
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.