javascript从aws lambda查询dynamodb中的最后一项

Kev*_*ang 2 javascript amazon-web-services amazon-dynamodb aws-lambda

所以我有一个带有主键ID和辅助键的 dynamodb 表,Time它是一个数字/时间戳。现在我想查询特定 ID 的最新记录。我试图构造这样的参数,但不知道如何继续查询具有最大Time.

function get_snapshot(id, callback){
    let params = {
        TableName: "Table1",
        KeyConditionExpression: "ID = :id and Time ", // here I stuck
        ExpressionAttributeValues: {
        ":id": id
        }
    }; 
    docClient.query(params, function(err, data){
        ... // process the fetched item here
    })
}
Run Code Online (Sandbox Code Playgroud)

我对这个领域很陌生。新手可能会犯很多错误。任何帮助表示赞赏。

Mat*_*ope 9

You don't actually need Time in the key condition for your query. If you want to get the 10 latest items, for example, your query params should look like this:

let params = {
    TableName: "Table1",
    KeyConditionExpression: "ID = :id",
    ExpressionAttributeValues: {
        ":id": id
    },
    ScanIndexForward: false,
    Limit: 1
}; 
Run Code Online (Sandbox Code Playgroud)

ScanIndexForward: false tells DynamoDB that it should return results starting with the highest sort key value. Limit: 1 tells DynamoDB that you only want to get the first 1 results.