【问题标题】:Find items where DynamoDB attribute is not set *or* greater than x查找未设置 DynamoDB 属性*或*大于 x 的项目
【发布时间】:2016-04-13 19:03:17
【问题描述】:

我正在尝试从 Lambda (Node.js) 扫描 DynamoDB 表以检查令牌是否已过期。我想过滤数据以排除设置了expiration_time 并且小于当前时间的项目。目前,对于未设置 expiration_time 的单位,我得到了误报。

如何检查过期的时间戳或根本没有设置expiration_time 属性?

dynamodb.scan({
    "TableName": "fm_tokens",
    "AttributesToGet": ["user_id"],
    "ScanFilter": {
        "token": {
            "AttributeValueList": [{"S": tkn.toString()}],
            "ComparisonOperator": "EQ"
        },
        "token_time": {
            "AttributeValueList": [{"N": expiration.toString()}],
            "ComparisonOperator": "GE"
        },
        "expiration_time": {
            "AttributeValueList": [{"N": unix_timestamp.toString()}],
            "ComparisonOperator": "GE",
        }
    }
}

【问题讨论】:

    标签: node.js amazon-web-services amazon-dynamodb aws-sdk aws-lambda


    【解决方案1】:

    事实证明,执行“OR”操作的最佳方法是使用新的、基于字符串的过滤命令。 AttributesToGet 替换为ProjectionExpression,ScanFilter 替换为FilterExpression。

    请注意,对于新命令,您将无法使用 reserved words,并且必须通过使用 ExpressionAttributeNames 定义这些键和使用 ExpressionAttributeValues 定义任何值来解决任何保留字。

    dynamodb.scan({
      // Define table to scan
        "TableName": "fm_tokens",
      // Only return selected values (optional)
        "ProjectionExpression": "user_id",
      // Write your expression, similar to SQL syntax
        "FilterExpression": 
            "#token = :tkn AND "+
            "token_time >= :expiration AND "+
            "(attribute_not_exists(expiration_time) OR expiration_time >= :timestamp)",
      // Use this to avoid reserved words by defining variables with the name instead
        "ExpressionAttributeNames": {
            "#token": "token"
        },
      // Same as with names, but for values - must include value type (S, N, etc.)
        "ExpressionAttributeValues": {
            ":tkn": {"S": tkn},
            ":expiration": {"N": expiration.toString()},
            ":timestamp": {"N": unix_timestamp.toString()}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2018-10-16
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多