我试图使用IN过滤器查询DynamoDB表。当我将单个值传递给IN时,我的查询起作用,但是当我传入多个时,我得不到任何匹配。使用IN过滤DynamoDB查询使用IN
这里是params对象的初始设置。
var params = {
TableName: "Interactions",
IndexName: "environment-time-index",
KeyConditionExpression: "#environment = :environment and #time between :start and :end",
FilterExpression: "#product = :product",
ExpressionAttributeNames: {
"#product": "product",
"#environment": "environment",
"#time": "time"
},
ExpressionAttributeValues: {
":product": product,
":environment": environment,
":start": start,
":end": end
}
};
接下来,如果用户提供了一个固定的查询参数,我修改了params对象,就像这样。这是我使用IN操作符的地方。
if (req.query.firm) {
var firms = req.query.firm;
console.log('debug', firms);
params.FilterExpression += " AND #firmCode IN (:firms)";
params.ExpressionAttributeNames["#firmCode"] = "firmCode";
params.ExpressionAttributeValues[":firms"] = firms;
}
最后,我运行查询,就像这样。
docClient.query(params, function(err, data) {
if (err) {
log.error(`Unable to scan. Error: ${JSON.stringify(err, null, 2)}`);
res.status(500).json({ error: "Oh, snap!" });
} else {
data.Parameters = params.ExpressionAttributeValues;
log.info(`Scan succeeded. Received ${data.Count} items.`)
res.send(data);
}
});
当公司参数包含单个值时,我会返回结果。
Level="INFO", Date="2016-09-19 14:26:03,373", Message="batchinsight received GET for /api/history/interactions2", Product="Exhaust", Service="batchinsight", AppDomain="Exhaust_batchinsight"
debug TW7ZN
Level="INFO", Date="2016-09-19 14:26:03,623", Message="Scan succeeded. Received 19 items.", Product="Exhaust", Service="batchinsight", AppDomain="Exhaust_batchinsight"
但是当它包含多个值时,我没有得到任何结果。
Level="INFO", Date="2016-09-19 14:35:16,896", Message="batchinsight received GET for /api/history/interactions2", Product="Exhaust", Service="batchinsight", AppDomain="Exhaust_batchinsight"
debug TW7ZN,TEXK4
Level="INFO", Date="2016-09-19 14:35:16,991", Message="Scan succeeded. Received 0 items.", Product="Exhaust", Service="batchinsight", AppDomain="Exhaust_batchinsight"
DynamoDB文档建议IN运算符可以接受以逗号分隔的值列表,但我无法使其工作。请帮忙! http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html#ConditionExpressionReference