Cmag
Cmag

Reputation: 15750

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!

Upvotes: 3

Views: 6335

Answers (2)

Patrick Wales
Patrick Wales

Reputation: 9

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."

You can find the amazon aws console by typing "amazon aws" into google.

Upvotes: -1

rpmartz
rpmartz

Reputation: 3809

Have you set up a global secondary index with make as the hash key of the index? Your query is telling DynamoDB to use an index named make-index, but it's not clear from your question whether that index exists.

The only way to use the Query operation in DynamoDB is using an EQ operator on a hash key. 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." If you have a global secondary index set up with make as the hash key, then you could perform the query you've described.

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.

Upvotes: 2

Related Questions