javascript - How to delete entry in dynamodb list? - Stack Overflow

I've got a lambda function, which acts as a trigger on a table with best scores of users to handle

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.

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.

Share Improve this question edited Jan 16, 2018 at 6:18 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Nov 2, 2017 at 10:01 KrystianKrystian 3,4233 gold badges39 silver badges77 bronze badges 4
  • You just need to delete one entry from list if it has multiple values? – notionquest Commented Nov 2, 2017 at 10:05
  • it doesn't matter, just delete the entry specified. It's always a list, if the list has no other values after I have removed the last one, I will detect that in the response data and will remove the entry altogether. – Krystian Commented Nov 2, 2017 at 10:17
  • 1 I would really appreciate a ment when downvoting my question. Why the downvote? Was it inappropriate? Lack of examples/code samples? – Krystian Commented Nov 2, 2017 at 15:21
  • A little outside the question, but using Redis is a good option for leaderboards. It has a type called a sorted set which is ideal for leaderboards. It makes adding and updating high scores incredibly simple, taking away the logic for this. But, understand if you want to keep your architecture simpler and potentially lower cost. – smcstewart Commented Nov 4, 2017 at 8:11
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309843a4621940.html

相关推荐

  • javascript - How to delete entry in dynamodb list? - Stack Overflow

    I've got a lambda function, which acts as a trigger on a table with best scores of users to handle

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信