Reputation: 45
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));
}
});
});
Upvotes: 0
Views: 1675
Reputation: 5747
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
.
Upvotes: 2