如果不是主要的或 sort_key 如何从 dynamodb 获取项目

How to get the item from dynamo db if its not primary or sort_key

我的 dynamodb table 有 user 作为 partition_keysort_key 作为 Time

我正在以指定格式从 dynamodb 中搜索 'Product Team' 中的 type

下面我从 dynamo 数据库中添加了 3 个项目 table

{ "user": "a@gmail.com", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested":"Requested for the 192.168.1.1 access" "Time": "2021-01-08 12:09:54.986542" },

{ "user": "a@gmail.com", "type": "Product Team", "message": "Developer", "employeeId": "101", "message_requested":"Requested for the 192.168.1.2 access" "Time": "2021-01-09 12:10:54.986542" },

{ "user": "c@gmail.com", "type": "Ops Team", "message": "Tester", "employeeId": "102", "message_requested":"Requested for the 192.168.1.1 access" "Time": "2021-01-08 11:09:54.986542" }

预计是

{"user":"a@gmail.com",
"params":[{"message_requested":"Requested for the 192.168.1.1 access"
  "Time": "2021-01-08 12:09:54.986542"},
{"message_requested":"Requested for the 192.168.1.2 access"
  "Time": "2021-01-09 12:10:54.986542"}]
}

下面是从 dynamodb 中提取项目的代码 table

def details(type):
    dynamodb = boto3.resource('dynamodb')
    client = boto3.client('dynamodb')
    table = dynamodb.Table("my_db_table_name")
    #res = client.get_item(
    #            TableName=table,
    #            Key={
    #                'type': {'S': 'Product Team'
    #                       }})
    res = table.scan(FilterExpression=Attr("type").eq("Product Team"))
    return res  

有两种选择:

  1. 使用扫描在 table 中执行搜索。在这种情况下搜索 与 Primary/Composite 密钥相比,时间将很重要。
  2. 做一个Global Secondary Index然后做 Primary/Composite 键入您需要的字段。