Reputation: 3423
I've got a lambda function, which acts as a trigger on a table with best scores of users to handle a leaderboard table.
In my leaderboard table, the sort key is the score, and the player's name is a separate entry with a list, because it's possible that there could be more than one player with the same score. Never mind.
So when adding a player I do:
var paramsNewEntry = {
"TableName": leaderboardTable,
"Key": {
"trackId": trackId,
"time": newValue
},
"UpdateExpression": "SET players = list_append(if_not_exists(players, :emptyList), :playersList),
"ExpressionAttributeValues": {
":playersList": [userId],
":emptyList":[]
},
"ReturnValues": "NONE"
};
And this works fine. I wanted to remove it this way:
var paramsOldEntry = {
"TableName": myTable,
"Key": {
"trackId": trackId,
"time": oldValue
},
"UpdateExpression": "DELETE players :playerToRemove",
"ExpressionAttributeValues": {
":playerToRemove": [userId]
},
"ReturnValues": "ALL_NEW"
}
But I get: Invalid UpdateExpression: Incorrect operand type for operator or function; operator: DELETE, operand type: LIST
error.
The players
attribute is a list, query response example:
{
"Items": [
{
"time": {
"N": "99994"
},
"players": {
"L": [
{
"S": "krystianPostman2"
}
]
},
"trackId": {
"S": "betaTrack001"
}
}
],
"Count": 1,
"ScannedCount": 1,
"LastEvaluatedKey": {
"time": {
"N": "99994"
},
"trackId": {
"S": "betaTrack001"
}
}
}
I've not seen any question on SO which would provide any details on this in javascript, when using the dynamodb Document API.
Upvotes: 3
Views: 5452
Reputation: 39186
DynamoDB API doesn't have an option to delete the value from LIST datatype based on its value. However, if you know the index of the value to be deleted, you can use REMOVE to delete the entry from list.
The DELETE action only supports Set data types.
UpdateExpression: 'REMOVE players[0]'
If the LIST is going to have only name
attribute, it is better to save it as SET rather than LIST DynamoDB datatype.
Creating Set:-
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.createSet( ["v1", "v2"]);
Deleting the values from SET using DELETE
Upvotes: 5