node.js - 使用全局二级索引的DynamoDB表查询项

标签 node.js amazon-web-services amazon-dynamodb

我正在尝试查询包含各个位置的纬度和经度的发电机表。我想当用户在 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/

相关文章:

amazon-dynamodb - 获取 dynamoDB 主键列表

java - DynamoDB - 查询而不是扫描 - 如何使用随机生成的 key 实现

node.js - CoreMongooseArray 到普通数组

javascript - express 服务器 : Error: Requested Range Not Satisfiable

node.js - 由于某种原因,bulkWrite 和 arrayFilter 没有修改文档。 MongoDB 和 Mongoose

amazon-web-services - 无法通过aws公共(public)IP连接到Elasticsearch

hadoop - 群集创建-AWS-EMR-*:9000出现连接异常失败:java.net.ConnectException:连接被拒绝;

amazon-web-services - 下载多个文件 S3

amazon-dynamodb - 通过 CDK 或 cloudformation 创建 DynamoDB 或其他有状态资源的标准方法

node.js - "Fetch failed loading"- 提取在 Postman 中有效,但在 Chrome 和 Safari 中失败