我有一个具有以下项目结构的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
}
]
}
-
{
"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
}
]
}
该表将以cart_id
作为主键,
user_id
作为索引或GSI,
type
作为索引或GSI。
我希望能够查询购物车表,
查找具有user_id = 1234 AND type != "SPECIAL"
的商品。
我不知道这是否意味着查询-
--key-condition-expression "user_id = 1234 AND type != 'SPECIAL'"
我了解无法同时使用多个索引查询AWS DynamoDb表,
我遇到了以下问题,它有一个类似的用例,答案是建议创建一个复合键,
Querying with multiple local Secondary Index Dynamodb
这是否意味着在将新项目放入表格中时,
我需要维护另一列,例如user_id_type
,
其值为1234SPECIAL
,并为user_id_type
创建一个索引/ GSI?
示例项目结构-
{
"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
}
]
}
参考-
1. Querying with multiple local Secondary Index Dynamodb
2. Is there a way to query multiple hash keys in DynamoDB?
答案 0 :(得分:1)
您的假设是正确的。也许您可以添加一个定界符field1_field2,或者如果它们中的任何一个的大小都太大,则对它们进行哈希处理hashOfField1_hashOfField2
但是,这意味着您需要花费更多的处理能力。由于DynamoDB本身不支持它。
Composite key in DynamoDB with more than 2 columns?
Dynamodb: query using more than two attributes
您的用例的其他信息
KeyConditionExpression仅允许用于哈希键。 您可以将其放入FilterExpression
Why is there no **not equal** comparison in DynamoDB queries?
答案 1 :(得分:0)
”这是否意味着在将新项目放入表格中时, 我将需要维护另一列,例如user_id_type, 其值为1234SPECIAL并为user_id_type创建索引/ GSI?“
答案是否取决于您需要的 多少列 (dynamodb的架构较少,按列表示数据字段),您是否对 2次来回 。
您的查询: user_id = 1234并输入!=“ SPECIAL”
1- 如果您需要购物车中的所有信息,但对两次往返都很满意:
2- 如果您不需要所有购物车信息。
3- 如果您只想一次往返,并且需要所有数据