-1

I need to get ALL the data (a large amount) from the past two months from a table in DynamoDB. As I don't need in this case the partition key in the condition, and Scan doesn't retrieve all the data, I've read that it's possible (if I'm not mistaken) to make a query with a secondary index, so I did but I keep getting this error:

{"message":"Query key condition not supported"}

May be there's something I'm missing? Do you recommend another approach? Thanks!

This is my code:

const dateFilter = {
            ':from_time': twoMonthsAgo,
            ':to_time': todayFormatted,
        };
const paramsQuery: DocumentClient.QueryInput = {
            TableName: JSON.parse(process.env.dynamoTables).myTable,
            IndexName: 'timeStamp-index',
            ExpressionAttributeNames: { '#time': 'timeStamp' },
            KeyConditionExpression: '#time BETWEEN :from_time and :to_time',
            ExpressionAttributeValues: dateFilter,
};

this.dynamoClient.query(paramsQuery, (error, data) => {
if (error) {
    callback(error, null);
} else {
    callback(null, this.operations);
}
});

Agostina
  • 29
  • 1
  • 3
  • 2
    Query command always requires the primary key https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html scan probably works but have a limit of 1 MB, per request, will need to paginate – Woohaik Oct 27 '22 at 14:18

1 Answers1

0

You cannot do a range query on a partition key, only on the sort key. Depending on the throughput your table has you can define a single PK for your index, lets say for example the Number 1.

GSI

PK    timeStamp
1     2022-10-01
1     2022-09-11
1     2022-12-01
1     2022-07-15
1     2022-10-01
const paramsQuery: DocumentClient.QueryInput = {
            TableName: JSON.parse(process.env.dynamoTables).myTable,
            IndexName: 'timeStamp-index',
            ExpressionAttributeNames: { '#time': 'timeStamp' },
            KeyConditionExpression: 'PK = 1 AND #time BETWEEN :from_time and :to_time',
            ExpressionAttributeValues: dateFilter,
};
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31