我正在尝试查询包含各个位置的纬度和经度的发电机表。我想当用户在 map 上平移时获取某些坐标之间的值。
表的主键是city
,排序键是id
。我创建了一个全局二级索引,其中 lat
作为分区键,lon
作为排序键(用于查询纬度和经度两点之间的位置)。
我正在尝试使用此查询:
let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
...
var params = {
TableName : "locations-dev",
IndexName: "lat-lon-index",
KeyConditionExpression: "lon between :lon2 and :lon1 AND lat between :lat1 and :lat2",
ExpressionAttributeValues: {
":lat1": JSON.stringify(event.bodyJSON.east),
":lat2": JSON.stringify(event.bodyJSON.west),
":lon1": JSON.stringify(event.bodyJSON.north),
":lon2": JSON.stringify(event.bodyJSON.south)
}
};
dynamo.query(params, function (err, data) {
if (err) {
console.error('Error with ', err);
context.fail(err);
} else {
context.succeed(data);
}
});
但我收到此错误:
{
"errorMessage": "Query key condition not supported",
"errorType": "ValidationException",
"stackTrace": [
...
]
}
以下是 Dynamo 中的示例项目:
{
"id": "18",
"lat": "39.923070",
"lon": "-86.036178",
"name": "Home Depot",
"phone": "(317)915-8534",
"website": "https://corporate.homedepot.com/newsroom/battery-recycling-one-million-pounds"
}
最佳答案
DynamoDB 中的主键(即使在辅助索引中)只能使用等于条件进行查询。该约束源自其内部表示,因为它存储为散列值以标识其项目分区。这些哈希值无法按范围查询。
Choosing the Right DynamoDB Partition Key
Except for scan, DynamoDB API operations require an equal operator (EQ) on the partition key for tables and GSIs. As a result, the partition key must be something that is easily queried by your application with a simple lookup (for example, using key=value, which returns either a unique item or fewer items).
关于node.js - 使用全局二级索引的DynamoDB表查询项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958379/