You have a DynamoDB table with a primary key id. You run a PutItem operation with an item that has the same id as an existing item but different attribute values. What will be the result?
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Alice'}}})
PutItem({TableName: 'Users', Item: {id: {S: '123'}, name: {S: 'Bob'}}})Think about how PutItem works with existing keys in DynamoDB.
PutItem replaces the entire item if the primary key already exists. It does not merge or add duplicates.
Choose the correct DynamoDB PutItem request syntax to add an item with id as a string and age as a number.
Remember DynamoDB expects attribute values to be typed with S for string and N for number, both as strings.
Option C uses the correct attribute value format: {S: 'string'} and {N: 'number_as_string'}.
You want to add a new item only if an item with the same primary key does not exist. Which option correctly achieves this using PutItem?
Think about how to tell DynamoDB to only put if the item does not exist.
The ConditionExpression with attribute_not_exists prevents overwriting by failing the operation if the key exists.
Consider this PutItem request:
{
TableName: 'Orders',
Item: {
orderId: {S: 'A123'},
amount: {N: 100}
}
}Why does it fail with a ValidationException?
Check the data types required by DynamoDB for number attributes.
DynamoDB requires number attributes to be strings representing numbers, so {N: '100'} is correct.
You perform a PutItem operation with ReturnValues set to 'ALL_OLD'. What will the response contain if the item already existed?
Think about what ReturnValues='ALL_OLD' means in DynamoDB PutItem.
When ReturnValues='ALL_OLD' is set, DynamoDB returns the entire item that was replaced if it existed before.