AWS Certified Developer Associate DVA-C02 Practice Question
A developer is working on an application that needs to store and retrieve player data for an online game. The game's backend is using AWS services for its infrastructure. The developer has chosen Amazon DynamoDB for storing player data. Each player has a 'PlayerID' as the unique identifier and various attributes like 'Score', 'Level', and 'Items'. The developer needs to write code to update a player's 'Score' after completing a game session. Which code snippet, assuming the AWS SDK for JavaScript is being used, correctly and efficiently updates the 'Score' attribute for a given 'PlayerID' without affecting other attributes?
dynamodb.getItem({TableName: 'Players', Key: {'PlayerID': playerId}}, function(err, data) { if (!err) { data.Item.Score = newScore; dynamodb.put({TableName: 'Players', Item: data.Item}); }})
dynamodb.put({TableName: 'Players', Item: {'PlayerID': playerId, 'Score': newScore}})
dynamodb.update({TableName: 'Players', Key: {'PlayerID': playerId}, UpdateExpression: 'REMOVE Score SET Score = :newScore', ExpressionAttributeValues: {':newScore': newScore}})
dynamodb.update({TableName: 'Players', Key: {'PlayerID': playerId}, UpdateExpression: 'SET Score = :newScore', ExpressionAttributeValues: {':newScore': newScore}})
dynamodb.scan({TableName: 'Players', FilterExpression: 'PlayerID = :playerId', ExpressionAttributeValues: { ':playerId': playerId }, ProjectionExpression: 'Score'}, function(err, data) { var items = data.Items; for (var i=0; i < items.length; i++) { if (items[i].PlayerID == playerId) { items[i].Score = newScore; } }})
dynamodb.update({TableName: 'Players', Key: {'PlayerID': playerId}, UpdateExpression: 'ADD Score :increment', ExpressionAttributeValues: {':increment': scoreIncrement}})