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?
Try ScanIndexForward and I bet it'll work. You transposed the two words.