Update Data
DynamoDB তে data update করার syntax
1️⃣ AWS CLI
aws dynamodb update-item \
--table-name Users \
--key '{"user_id": {"S": "1"}}' \
--update-expression "SET age = :newAge, city = :newCity" \
--expression-attribute-values '{":newAge":{"N":"25"}, ":newCity":{"S":"Chattogram"}}' \
--endpoint-url http://localhost:8000
Explanation:
--table-name→ Table name--key→ Update করার record এর Partition Key (Sort Key থাকলে দিতে হবে)--update-expression→ কোন attribute update করতে হবে--expression-attribute-values→ নতুন value define করে দেওয়া--endpoint-url→ Local DynamoDB address
Optional: ConditionExpression use করলে update হবে শুধুমাত্র যদি কিছু condition satisfy হয়।
2️⃣ Python
import boto3
dynamodb = boto3.resource('dynamodb', endpoint_url='http://localhost:8000', region_name='us-east-1')
table = dynamodb.Table('Users')
response = table.update_item(
Key={'user_id': '1'}, # Partition Key
UpdateExpression="SET age = :newAge, city = :newCity",
ExpressionAttributeValues={
':newAge': 25,
':newCity': 'Chattogram'
},
ReturnValues="UPDATED_NEW" # Return only updated attributes
)
print("Updated attributes:", response['Attributes'])
Explanation:
Key→ Update করার record এর primary keyUpdateExpression→ কোন attribute update হবে, কীভাবেExpressionAttributeValues→ নতুন value mappingReturnValues→ updated attributes ফেরত পেতে
3️⃣ Notes
DynamoDB তে partial update possible → শুধু যেটা change করতে চাও, update হবে
Numeric type increment/decrement করা সম্ভব:
table.update_item(
Key={'user_id': '1'},
UpdateExpression="SET age = age + :inc",
ExpressionAttributeValues={':inc': 1},
ReturnValues="UPDATED_NEW"
)
ConditionExpression দিয়ে ensure করতে পারো update হবে শুধুমাত্র যদি কিছু শর্ত satisfy হয়:
table.update_item(
Key={'user_id': '1'},
UpdateExpression="SET city = :newCity",
ExpressionAttributeValues={':newCity': 'Sylhet'},
ConditionExpression="attribute_exists(user_id)", # Only if record exists
ReturnValues="UPDATED_NEW"
)
✅ Summary
| Operation | AWS CLI | Python boto3 | Notes |
|---|---|---|---|
| Update data | update-item | update_item() | Partial update possible, ReturnValues optional |
| Increment numeric | CLI + SET age = age + :inc | UpdateExpression="SET age = age + :inc" | Auto increment |
| Conditional update | --condition-expression | ConditionExpression=... | Safe update |