[英]javascript query last items in dynamodb from aws lambda
So I have a dynamodb table with a primary key ID
and a secondary key Time
which is a number/timestamp.所以我有一个带有主键ID
和辅助键Time
的 dynamodb 表,它是一个数字/时间戳。 Now I want to query the latest record of a particular ID.现在我想查询特定 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
.我试图构造这样的参数,但不知道如何继续查询具有最大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.任何帮助表示赞赏。
You don't actually need Time
in the key condition for your query.您实际上不需要在查询的关键条件中使用Time
。 If you want to get the 10 latest items, for example, your query params should look like this:例如,如果您想获取 10 个最新项目,您的查询参数应如下所示:
let params = {
TableName: "Table1",
KeyConditionExpression: "ID = :id",
ExpressionAttributeValues: {
":id": id
},
ScanIndexForward: false,
Limit: 1
};
ScanIndexForward: false
tells DynamoDB that it should return results starting with the highest sort key value. ScanIndexForward: false
告诉 DynamoDB 它应该返回从最高排序键值开始的结果。 Limit: 1
tells DynamoDB that you only want to get the first 1 results. Limit: 1
告诉 DynamoDB 您只想获得前 1 个结果。
Just to add, the ScanIndexForward is a boolean, and isn't supposed to be quoted.补充一点, ScanIndexForward 是一个布尔值,不应该被引用。
So所以
let params = {
TableName: "Table1",
KeyConditionExpression: "ID = :id",
ExpressionAttributeValues: {
":id": id
},
ScanIndexForward: false,
Limit: 1
};
Thanks for this answer 🙏谢谢这个回答🙏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.