【问题标题】:filter data using multiple conditions dynamodb使用多个条件 dynamodb 过滤数据
【发布时间】:2021-02-25 23:47:15
【问题描述】:

如何在 dynamodb 中使用多个条件过滤数据。

我想使用 scan 方法按 post_date 和 region 过滤表。

var params = {
    TableName: table,
    KeyConditionExpression : 'post_date = :today_date',
    FilterExpression : 'post_date = :today_date and district = :district',
    ExpressionAttributeValues : {
        ':today_date' : today_date,
        ':district' : district
    }
};

let queryExecute = new Promise((res, rej) => {
    dynamoDB.scan(params, function (err, data) {
        if (err) {
            console.log("Error", err);
            rej(err);
        } else {
            console.log("Success! scan method fetch data from dynamodb");
            res(JSON.stringify(data, null, 2));
        }
    });
});

【问题讨论】:

    标签: node.js express amazon-dynamodb


    【解决方案1】:

    可以使用扫描操作过滤多个属性:

    var params= {
        TableName: "YOUR TABLE NAME",
        FilterExpression : 'post_date = :today_date and district = :district',
        ExpressionAttributeValues : {
            ':today_date' : today_date,
            ':district' : district
        }
      }
    

    您的示例包含一个KeyConditionExpressionscan 操作不支持它。如果您知道所需项目的主键,则应该使用query 操作。

    scan 操作是您想要搜索表中所有分区时使用的操作。 query 操作是您在特定分区中搜索内容时使用的操作。

    由于您尝试执行scan 操作,您需要删除KeyConditionExpression

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2023-01-04
      • 1970-01-01
      • 2018-08-24
      • 2020-08-29
      相关资源
      最近更新 更多