Search code examples
pythonamazon-web-servicesamazon-dynamodbboto3epoch

search records of a user from last 30 days dynamodb


my dynamodb structure is somehow for example my PK and SK is like

PK = USR#123ACT#234 and SK = AYT#<epoch_time>

and my global index are like

GSi3pk = ACT#234 and GSI3SK = <epoch_time>

now case is using global index I want to get data of last 30 days with account_id=234


Solution

  • To query for a specific primary key (of account number) and a timestamp in the last 30 days, you can query with some variant of this:

    {
        "TableName": "orders",
        "IndexName": "gsi3",
        "KeyConditionExpression": "gsi3pk = :pk AND tstamp BETWEEN :start AND :end",
        "ExpressionAttributeValues": {
            ":pk": {
                "S": "ACT#234"
            },
            ":start": {
                "N": "1651962700"
            },
            ":end": {
                "N": "1654641000"
            }
        }
    }