简体   繁体   English

如何将“db.query”用于NodeJS AWS-SDK for DynamoDB

[英]How to use 'db.query' for the NodeJS AWS-SDK for DynamoDB

Folks, I have the following table: 伙计们,我有下表:

  hashKey (model):   RangeKey (make):
  mustang            ford
  f150               ford
  malibu             chevy
  ...

What I would like to do is search and return all Ford models, ie: 我想做的是搜索并返回所有福特车型,即:

var params = {
        TableName : 'models',
        IndexName : 'make-index',
        KeyConditions : 
        {
            "make" : 
            {
                "AttributeValueList" : [
                {
                    "S" : 'ford'
                }
                ],
                "ComparisonOperator" : "EQ"
            }
        },
    }
    db.query(params, function(err, data) {
        if (err) {
            console.log (err)
            callback(err, null)
        } else {
            callback(null, data.Items)
        }
    });

What am I doing wrong? 我究竟做错了什么? :) Thanks! :) 谢谢!

Have you set up a global secondary index with make as the hash key of the index? 您是否使用make作为索引的哈希键来设置全局二级索引? Your query is telling DynamoDB to use an index named make-index , but it's not clear from your question whether that index exists. 您的查询告诉DynamoDB使用名为make-index ,但是从您的问题中不清楚该索引是否存在。

The only way to use the Query operation in DynamoDB is using an EQ operator on a hash key. 在DynamoDB中使用Query操作的唯一方法是在散列键上使用EQ运算符。 That could be on the hash key on the table, or a hash key on a global secondary index, but there's no way (without a global secondary index) to construct a query that says "give me all the items whose range key is ford ." 这可能是关于表上的哈希键,或者是全局二级索引上的哈希键,但是没有办法(没有全局二级索引)构造一个查询“给我所有范围键为ford的项目”。 “ If you have a global secondary index set up with make as the hash key, then you could perform the query you've described. 如果您将全局二级索引设置为使用make作为哈希键,则可以执行您所描述的查询。

Unfortunately, indexes can only be created when the table is created. 不幸的是,只能在创建表时创建索引。 If you don't have a global secondary index on the make item, then you could accomplish it with a Scan operation, but it won't be as performant as a Query . 如果make项上没有全局二级索引,那么可以使用“ Scan操作完成它,但它不会像Query那样Query

You can make global secondary indexes after a table is made. 您可以在创建表后创建全局二级索引。 Go to the amazon aws dynamodb console, click on a table and click "create index." 转到amazon aws dynamodb控制台,单击表格并单击“创建索引”。

You can find the amazon aws console by typing "amazon aws" into google. 您可以通过在Google中输入“amazon aws”找到amazon aws控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM