使用SET更新列表元素时,该元素的内容将替换为指定的新数据。如果元素不存在,SET会将新元素追加到列表的末尾。
创建表格
aws dynamodb create-table \
--table-name article \
--attribute-definitions AttributeName=article_id,AttributeType=N \
--key-schema AttributeName=article_id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1
添加项
aws dynamodb put-item \
--table-name article \
--item '{
"article_id": {"N": "123"},
"updated": {"S": "00:00:00"},
"comments": {
"L": [
{ "S": "Nice article!" },
{ "S": "Thank you!" }
]}
}'
更新项目
aws dynamodb update-item \
--table-name article \
--key '{"article_id":{"N":"123"}}' \
--update-expression "SET comments[50] = :c, updated=:u" \
--expression-attribute-values '{
":u": {"S": "01:01:01"},
":c": {"S": "This is the new one!"}
}' \
--return-values ALL_NEW
列表
comments[]
在更新之前包含两个元素(索引0和1)。像
comments[50]
如果不存在,则SET操作会将其附加到列表的末尾(
comments[2]
).