“ IN” dynamodb中的声明 [英] "IN" statement in dynamodb
问题描述
我有一个用户表,下面是一个示例:
I have a "Users" table, here is a sample :
{
username:"haddox",
formattedPhoneNumber:"676767676",
verified: 0,
}
我希望检索其formattedPhoneNumber包含在电话号码数组中的所有用户(从我的联系人中检索)。我创建了一个二级索引,已验证为HASH,formattedPhoneNumber为RANGE。这是我的尝试:
My wish is to retrieve all users whose formattedPhoneNumber is contained in an array of phone numbers (retrieved from my contacts). I created a secondary index, with verified as HASH and formattedPhoneNumber as RANGE. Here is my try :
var params = {
TableName: "Users",
IndexName: "FormattedPhoneSecondaryIndex",
KeyConditionExpression: "verified = :v AND formattedPhone IN :n",
ExpressionAttributeValues: {
":v":1,
":n": ["672053916", "642117296"]
},
ProjectionExpression: "username, formattedPhoneNumber"
};
dynamodb.query(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
但是出现以下错误: Invalid KeyConditionExpression:语法错误;令牌:\:n\,附近:\ IN:n\,
是否出了点问题用IN关键字?
也许还有另一种方法可以实现此目的?
Is there something wrong with the IN keyword ? Maybe there is another way to achieve this ?
推荐答案
KeyConditionExpression不能使用 IN运算符(请参见 http://docs.aws.amazon.com/ amazondynamodb / latest / developerguide / QueryAndScan.html#FilteringResults )。在查询操作中使用KeyConditions / KeyConditionExpression的想法是从DynamoDB中更有效地读取项目页面,因为具有相同哈希键但范围键不同的项目是IN操作符需要提取某些页面的一小部分,这会使Query操作的效率降低,因此在KeyConditions中是不允许的,而您希望将其添加为FilterExpression,这是很方便的参数以减少数量从DynamoDB返回的所有项目,但不影响从DynamoDB读取数据的方式。
KeyConditionExpression's cannot use the "IN" operator (see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#FilteringResults). The idea with KeyConditions/KeyConditionExpression in a query operation is to more efficiently read pages of items from DynamoDB, since items with the same hash key but different range keys are stored contiguously and in sorted order. The IN operator would require extracting small portions of certain pages, which makes the Query operation less efficient, so it is not allowed in KeyConditions. You would want to add that as a FilterExpression instead, which is a convenience parameter to reduce the number of items returned from DynamoDB, but does not impact how the data is read from DynamoDB.
这篇关于“ IN” dynamodb中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!