如何使用多个索引查询 AWS DynamoDB?

Ani*_*aje 6 amazon-web-services nosql amazon-dynamodb

我有一个具有以下项目结构的 AWS DynamoDb 购物车表 -

{
  "cart_id": "5e4d0f9f-f08c-45ae-986a-f1b5ac7b7c13",
  "user_id": 1234,
  "type": "OTHER",
  "currency": "INR",
  "created_date": 132432423,
  "expiry": 132432425,
  "total_amount": 90000,
  "total_quantity": 2,  
  "items": [
    {
      "amount": 90000,
      "category": "Laptops",
      "name": "Apple MacBook Pro",
      "quantity": 1
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

——

{
  "cart_id": "12340f9f-f08c-45ae-986a-f1b5ac7b1234",
  "user_id": 1234,
  "type": "SPECIAL",
  "currency": "INR",
  "created_date": 132432423,
  "expiry": 132432425,
  "total_amount": 1000,
  "total_quantity": 2,  
  "items": [
    {
      "amount": 1000,
      "category": "Special",
      "name": "Special Item",
      "quantity": 1
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

该表将cart_id作为主键、
user_id作为索引或 GSI、
type作为索引或 GSI。

我希望能够查询购物车表,
以找到具有user_id = 1234 AND type != "SPECIAL".
我不知道这是否意味着查询 -

--key-condition-expression "user_id = 1234 AND type != 'SPECIAL'" 
Run Code Online (Sandbox Code Playgroud)

我知道不能同时使用多个索引查询 AWS DynamoDb 表
我遇到了以下问题,它有一个类似的用例,答案是建议创建一个复合键,
使用多个本地二级索引 Dynamodb 查询

这是否意味着在将新项目放入表中时,
我需要维护另一列,如user_id_type
其值为 as1234SPECIAL并为其创建索引/GSI user_id_type

示例项目结构 -

{
  "cart_id": "5e4d0f9f-f08c-45ae-986a-f1b5ac7b7c13",
  "user_id": 1234,
  "type": "OTHER",
  "user_id_type" : "1234OTHER",
  "currency": "INR",
  "created_date": 132432423,
  "expiry": 132432425,
  "total_amount": 90000,
  "total_quantity": 2,  
  "items": [
    {
      "amount": 90000,
      "category": "Laptops",
      "name": "Apple MacBook Pro",
      "quantity": 1
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

参考资料 -
1.使用多个本地二级索引 Dynamodb查询
2.有没有办法在 DynamoDB 中查询多个哈希键?

qkh*_*pro 3

你的假设是正确的。也许您可以添加一个分隔符 field1_field2 或对它们进行哈希处理(如果它们中的任何一个的大小太大) hashOfField1_hashOfField2

然而,这意味着您需要花费更多的处理能力。由于 DynamoDB 本身并不支持它。

DynamoDB 中的复合键具有超过 2 列?

Dynamodb:使用两个以上属性的查询

有关您的用例的附加信息

KeyConditionExpression 仅允许使用哈希键。您可以将其放入 FilterExpression 中

为什么 DynamoDB 查询中没有**不相等**比较?