最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - javascript query last items in dynamodb from aws lambda - Stack Overflow

programmeradmin1浏览0评论

So I have a dynamodb table with a primary key ID and a secondary key Time which is a number/timestamp. Now I want to query the latest record of a particular ID. I tried to construct the parameters like this but don't know how to move forward to query the item with the largest 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
    })
}

I'm quite new to this field. There could be a lot of rookie mistakes. Any help is appreciated.

So I have a dynamodb table with a primary key ID and a secondary key Time which is a number/timestamp. Now I want to query the latest record of a particular ID. I tried to construct the parameters like this but don't know how to move forward to query the item with the largest 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
    })
}

I'm quite new to this field. There could be a lot of rookie mistakes. Any help is appreciated.

Share Improve this question asked Nov 27, 2018 at 6:20 Kevin FangKevin Fang 2,0124 gold badges17 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

You don't actually need the Time attribute 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: 10
}; 

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 10 results.

Just to add, the ScanIndexForward is a boolean, and isn't supposed to be quoted.

So

let params = {
    TableName: "Table1",
    KeyConditionExpression: "ID = :id",
    ExpressionAttributeValues: {
        ":id": id
    },
    ScanIndexForward: false,
    Limit: 1
}; 

Thanks for this answer

发布评论

评论列表(0)

  1. 暂无评论