如何使用 Lambda 和 DynamoDB 在日期范围之间进行扫描?

How to scan between date range using Lambda and DynamoDB?

我正在尝试使用 Node Lambda 函数在日期范围之间进行扫描。我已正确扫描数据,但似乎无法使日期表达式正常工作。

var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function(event, context) {
    var tableName = "MyDDBTable";
    dynamodb.scan({
        TableName : tableName,
        FilterExpression: "start_date < :start_date",
        ExpressionAttributeValues: {
            ":start_date": {
                "S": "2016-12-01"
            }
        }
    }, function(err, data) {
        context.succeed(data);
    });
};

这目前不会尝试 return 一个范围,它现在只是在查看一个日期。我不想在表达式中添加 and,直到我知道这是有效的。

我的 DynamoDB 中的示例文档结构如下:

{
    "end_date": {
        "S": "2016-12-02"
    },
    "name": {
        "S": "Name of document"
    },
    "start_date": {
        "S": "2016-10-10"
    },
    "document_id": {
        "N": "7"
    }
}

document_id 是我的主键。我对整个 Lamdba / DynamoDB 组合还很陌生,所以我可能完全设置错误,但这是我通过研究设法完成的。

我最终想要实现的目标是给定一个开始日期和一个结束日期,return 所有具有该日期范围的 DynamoDB 文档。任何帮助将不胜感激。

首先,扫描操作是正确的。 dynamodb.scan 应该在循环中执行,直到 LastEvaluatedKey 不可用。请参考这个 blog.

lambda 没有return结果,因为它不会在第一次扫描中找到数据。如果您可以将扫描扩展到 LastEvaluatedKey 不可用,则 lambda 很可能 return 结果。

For Query and Scan operations, DynamoDB calculates the amount of consumed provisioned throughput based on item size, not on the amount of data that is returned to an application.

If you query or scan for specific attributes that match values that amount to more than 1 MB of data, you'll need to perform another Query or Scan request for the next 1 MB of data. To do this, take the LastEvaluatedKey value from the previous request, and use that value as the ExclusiveStartKey in the next request. This approach will let you progressively query or scan for new data in 1 MB increments.

BETWEEN 运算符示例:-

FilterExpression: "start_date BETWEEN :date1 and :date2"