Bird
0
0

You want to update an item attribute in DynamoDB using Boto3. Which approach correctly uses the resource and client to update the attribute 'Age' to 30 for UserId '123'?

hard🚀 Application Q15 of 15
DynamoDB - with AWS SDK
You want to update an item attribute in DynamoDB using Boto3. Which approach correctly uses the resource and client to update the attribute 'Age' to 30 for UserId '123'?
AResource: <code>table.update_item(Key={'UserId': '123'}, UpdateExpression='SET Age = :val', ExpressionAttributeValues={':val': 30})</code> Client: <code>client.update_item(TableName='Users', Key={'UserId': {'S': '123'}}, UpdateExpression='SET Age = :val', ExpressionAttributeValues={':val': {'N': '30'}})</code>
BResource: <code>table.update_item(Key={'UserId': {'S': '123'}}, UpdateExpression='SET Age = :val', ExpressionAttributeValues={':val': {'N': '30'}})</code> Client: <code>client.update_item(TableName='Users', Key={'UserId': '123'}, UpdateExpression='SET Age = :val', ExpressionAttributeValues={':val': 30})</code>
CResource: <code>table.put_item(Item={'UserId': '123', 'Age': 30})</code> Client: <code>client.put_item(TableName='Users', Item={'UserId': {'S': '123'}, 'Age': {'N': '30'}})</code>
DResource: <code>table.update_item(Key={'UserId': '123'}, UpdateExpression='ADD Age :val', ExpressionAttributeValues={':val': 30})</code> Client: <code>client.update_item(TableName='Users', Key={'UserId': {'S': '123'}}, UpdateExpression='ADD Age :val', ExpressionAttributeValues={':val': {'N': '30'}})</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand resource update_item usage

    Resource expects simple Python types for Key and ExpressionAttributeValues, so {'UserId': '123'} and {':val': 30} are correct.
  2. Step 2: Understand client update_item usage

    Client requires attribute types in Key and ExpressionAttributeValues, so {'UserId': {'S': '123'}} and {':val': {'N': '30'}} are needed.
  3. Step 3: Confirm UpdateExpression syntax

    Using SET Age = :val is correct for updating an attribute value.
  4. Final Answer:

    Resource and client update_item calls with correct Key and ExpressionAttributeValues formats as in option A -> Option A
  5. Quick Check:

    Resource uses plain types, client uses typed dicts [OK]
Quick Trick: Resource uses plain dicts; client needs typed attribute dicts [OK]
Common Mistakes:
MISTAKES
  • Mixing client and resource Key formats
  • Using ADD instead of SET for update
  • Passing raw values in client ExpressionAttributeValues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More DynamoDB Quizzes