I'm using dynamoDb to store some additional info but I have some troubles sorting my data.
I have the following create syntax where I have a storeId and number keys. I set the number as sort key, but the problem is that the data isn't sorted at all.
$response = $dynamoDb->createTable([
'TableName' => 'foo',
'KeySchema' => [
[
'AttributeName' => 'storeId',
'KeyType' => 'HASH' //Partition key
],
[
'AttributeName' => 'number',
'KeyType' => 'RANGE' // sort Key
]
],
'AttributeDefinitions' => [
[
'AttributeName' => 'storeId',
'AttributeType' => 'N'
],
[
'AttributeName' => 'number',
'AttributeType' => 'N'
]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 20,
'WriteCapacityUnits' => 20
]
]);
My scan params:
$scanParams = [
'TableName' => 'foo',
'ProjectionExpression' => '#storeId, #number',
'FilterExpression' => '#number >= :n',
'ExpressionAttributeNames'=> [ '#storeId' => 'storeId', '#number' => 'number'],
'ExpressionAttributeValues' => [
':n' => ['N' => $number]
]
];
The result of my scan:
StoreId number
68001 80000
25000 37000
463501 527000
4800001 5300000
360001 400000
2000001 2600000
As you can see, the data isn't sorted on the number property.
storeIdvalue. Essentially you can only get sorted results of a partition, not the entire table.storeIdand both keys needs to be specified in the query