如何使用AWS-lambda中的nodeJS对dynamo DB中的数据进行排序

时间:2019-04-17 05:53:32

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

我正在使用lambda函数中的onScan从表中检索数据。我想根据时间戳对它们进行排序。请为此提供建议

var params = {
    TableName: "LogEvents",
    FilterExpression: "#Environment = :env",
    ExpressionAttributeNames: {
       "#Environment": "Environment",
    }   
};

1 个答案:

答案 0 :(得分:0)

希望以下示例对您有帮助。

var params = {
        TableName: "Table",
         IndexName: "DataID-Created-index",
          KeyConditionExpression: "DataID = :v_ID AND 
         Created > :v_created",
         ExpressionAttributeValues: {":v_ID": {S: "some_id"},
                            ":v_created": {N: "timestamp"}
    },
      ProjectionExpression: "ID, DataID, Created, Data"
   };


  ddb.query(params, function(err, data) {
      if (err) 
          console.log(err);
      else {
           data.Items.sort(function(a, b) {
             return parseFloat(a.Created.N) - 
       parseFloat(b.Created.N);
            });
         // More code here
        }
       });