DynamoDB - 如何获取具有任何分区键但具有一系列排序键值的所有项目?

DynamoDB - How to get all items with any partition key, but with a range of sort key values?

我有一个 DynamoDB table,里面有一堆口袋妖怪,嘻嘻,我正在尝试按级别排序。

我有一个 GSI,其中 pokemon(名称、皮卡丘等)作为分区键,级别(数字值)作为排序键。

我想查询某个级别以上的所有人。这是我的代码:

db.query({
  TableName: "pokemon",
  IndexName: "level",
  KeyConditionExpression: `lv > :zero`,
  ExpressionAttributeValues: {
    ":zero": 0
  }
}, function (error, reply) {
  return console.log(error || reply)
})

我收到这个错误

ValidationException: Query condition missed key schema element: pokemon

我也试过这个

db.query({
  TableName: "pokemon",
  IndexName: "level",
  KeyConditionExpression: `pokemon = not_null and lv > :zero`,
  ExpressionAttributeValues: {
    ":name": 'not_null',
    ":zero": 0
  }
}, function (error, reply) {
  return console.log(error || reply)
})

并得到这个错误:

ValidationException: Invalid condition in KeyConditionExpression: Multiple attribute names used in one condition

我似乎无法为分区键取任何值。它一直对我大喊大叫:(

您需要使用 KeyConditionExpression 参数为分区键提供特定值。

您的查询应如下所示(伪代码):

{
  "TableName": "pokem",
  "IndexName": "level",
  "KeyConditionExpression": "#name = :name And #level > :level",
  "ExpressionAttributeValues": {
    ":name": "name",
    ":level": "level"
  },
  "ExpressionAttributeNames": {
    "#name": "pikachu",
    "#level": "0"
  }
}