在 AWS DynamoDB 查询中,是否可以将字符串查询为数字?

In AWS DynamoDB query, is it possible to query a string as a number?

在 AWS DynamoDB 查询中,是否可以将字符串作为数字进行查询?

在我的例子中,我有一个 myDate 字段,其中包含从 1970 年开始的秒数,格式为字符串。它也是全局二级索引上的排序键。

我需要 return 来自 DynamoDb 的所有记录,其中 myDate 在 2 个查询参数之间:fromDate 和 toDate。 问题是 myDate 是一个字符串,所以我不能轻易地比较这 3 个值。 目前我有一个看起来像这样的查询:

var params = {
                TableName: "MyTable",
                IndexName: "GSI",
                KeyConditionExpression: "primaryKey = :primaryKey AND myDate BETWEEN :fromDate AND :toDate",
                ExpressionAttributeValues: {
                    ":primaryKey ": {"N": "1"},
                    ":fromDate": {"S": "1550637900"},
                    ":toDate": {"S": "1550639400"}
                }
}

然而,由于字符串比较,这将无法正常工作。例如“2”>“10”。 那么有没有办法对 myDate 进行数字比较?

亚马逊特别提到它不能直接:

BETWEEN : Greater than or equal to the first value, and less than or equal to the second value.

AttributeValueList must contain two AttributeValue elements of the same type, either String, Number, or Binary (not a set type). A target attribute matches if the target value is greater than, or equal to, the first element and less than, or equal to, the second element. If an item contains an AttributeValue element of a different type than the one provided in the request, the value does not match. For example, {"S":"6"} does not compare to {"N":"6"}. Also, {"N":"6"} does not compare to {"NS":["6", "2", "1"]}

但也许有更好的方法我没有看到?

不,你不能。

如您所见,暂时不会有问题。多长时间取决于...如果您正在处理 32 位 Unix/Linux 纪元值...那么 2038

中的那些 运行

否则,您不必担心直到 2286,当您达到 10000000000(11 位)

以后考虑将dates/times存储为ISO格式的字符串,"2019-07-29T12:37:54Z""20190729T123754Z"

会照顾你9999年