'Sorting by partition key/sort key does not work

I'm working on an IoT project where I need to read some data from a device. I use AWS, and I'm currently working on some lambda function code. But I can't figure out how to get the last (newest) item from my database.

My database has two keys:

Partition key: device_id (Number)

Sort key sample_time (Number)

This is a part of the code I wrote to retrieve the newest reading from my IoT device

case "GET /data/newest":
        body = await dynamo
            .query({ 
              TableName: "bikelock_db",
              KeyConditionExpression: 'device_id = :id',
              ExpressionAttributeValues: {
                ":id": 1,
              },
              Limit: 1,
              ScanForwardIndex: false,
            })
            .promise();
        break;

This code however, only returns the first added item from the database. Changing the ScanForwardIndex: false to true doesn't change a thing. I thought the Sort Key would sort it automatically, but it does not.

Any idea what I'm missing, or why it isn't working?



Solution 1:[1]

Try ScanIndexForward and I bet it'll work. You transposed the two words.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hunterhacker