如何在 dynamodb 中使用多个条件过滤数据。
我想使用扫描方法按 post_date 和地区过滤表。
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));
}
});
});
您可以使用扫描操作过滤多个属性:
var params= {
TableName: "YOUR TABLE NAME",
FilterExpression : 'post_date = :today_date and district = :district',
ExpressionAttributeValues : {
':today_date' : today_date,
':district' : district
}
}
您的示例包含一个 KeyConditionExpression
,scan
操作不支持它。如果您知道所需项目的主键,则应该使用 query
操作。
scan
操作是您想要搜索表中所有分区时使用的操作。 query
操作是您在特定分区内搜索内容时使用的操作。
由于您正在尝试执行 scan
操作,因此您需要删除 KeyConditionExpression
。