0

How can I filter data using multiple conditions in dynamodb.

I want to filter a table by post_date and district using scan method.

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));
        }
    });
});

1 Answer 1

2

You can filter on multiple attributes using the scan operation:

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

Your example includes a KeyConditionExpression, which is not supported for the scan operation. If you know the primary key of the item you want, you should be using the query operation.

The scan operation is what you use when you want to search across all partitions in your table. The query operation is what you use if you what to search within a specific partition.

Since you are trying to execute a scan operation you'll need to remove the KeyConditionExpression.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.