首页 > 解决方案 > 如何根据时间戳和此时间戳值的偏移量获取 DynamoDB 表过滤器中的项目?

问题描述

我有具有以下架构的 DynamoDB 表:

var params = {
    TableName : "SomeTable",
    KeySchema: [       
        { AttributeName: "userid", KeyType: "HASH"},  //Partition key
        { AttributeName: "transactionId", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "userid", AttributeType: "S" },
        { AttributeName: "transactionId", AttributeType: "S" },
    ],
    GlobalSecondaryIndexes: [ 
        { 
           IndexName: "TransactionIndex",
           KeySchema: [ 
              { 
                 AttributeName: "transactionId",
                 KeyType: "HASH"
              },
              { 
                AttributeName: "productId",
                KeyType: "RANGE"
             }
           ],
           Projection: { 
              ProjectionType: "ALL"
           },
           ProvisionedThroughput: { 
              ReadCapacityUnits: 10,
              WriteCapacityUnits: 10
           }
        }
     ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

我正在查询这个表,根据时间值进行过滤,但是我找不到任何关于如何在 FilterExpression 中修改这个值的文档。这个想法是找到所有 expiry_date_ms 值即将到期或刚刚到期(即前 1 天或后 1 天)的行,如下所示:

var startoffset = 86400000;         // one day before
var endoffset = 86400000;         // one day after

var params = { 
    TableName: "SomeTable", 
    FilterExpression: "#tm between :start and :end'",    
    ExpressionAttributeNames: { "#tm": "expiry_date_ms" }, 
    ExpressionAttributeValues:{ ":start": "expiry_date_ms" - startoffset, ":end": "expiry_date_ms" + endoffset }  
}; 

docClient.scan(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log(params);
        console.log("Scan succeeded.");        
        data.Items.forEach(function(item) {
            console.log("Item :" + JSON.stringify(item));           
        });
    }
});

也许我的逻辑完全错误,并且有更好/更聪明的方法来运行此类查询,或者在我的情况下,最好的方法是扫描或查询。任何有关该问题的信息将不胜感激。

标签: node.jsamazon-dynamodbdynamodb-queries

解决方案


推荐阅读