如何基于TimeStamp和此TimeStamp值的偏移量在DynamoDB表过滤器中获取项目?

时间:2019-04-19 19:49:55

标签: node.js amazon-dynamodb dynamodb-queries

我有具有以下架构的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));           
        });
    }
});

也许我的逻辑完全错了,并且有更好/更聪明的方式来运行这种查询,或者最好的方法是“扫描”或“查询”。有关此问题的任何信息将不胜感激。

0 个答案:

没有答案