10

I try to query my dynamoDB from a Lambda function. My table uses "id" as the hash key. I tried both versions below and received the corresponding error messages. What am I doing wrong?

  var params = {
        TableName : "addresses",
        KeyConditionExpression: "id = :id AND city = :city",
        ExpressionAttributeValues: {
            ":id": "Austria",
            ":city": "Salzburg"
        }
    };

Unable to query. Error: { "message": "Query key condition not supported",...}

var params = {
    TableName : "addresses",
    KeyConditionExpression: "city = :city",
    ExpressionAttributeValues: {
        ":city": "Salzburg"
    }
};

Unable to query. Error: { "message": "Query condition missed key schema element: id",...}

EDIT:

I now added secondary indices, but still get the same errors:

enter image description here

3
  • quering for KeyConditionExpression: "id = :id" works? and do you have range key? Commented Feb 10, 2016 at 14:44
  • Thanks for the hint. I just tried only using the id without the city condition and it works. How can I add additional conditions? I do not use a range key Commented Feb 10, 2016 at 14:48
  • i will answer it (to get +1 :) ) Commented Feb 10, 2016 at 14:48

1 Answer 1

13

if your hash key is 'id' then you cant query by:

KeyConditionExpression: "id = :id AND city = :city"

or by:

KeyConditionExpression: "city = :city"

you can query dynamodb only by hash and range key.

so your query should contain always hash key (id). if you want to query by 'city' also, you should add 'city' as range key to dynamodb table (or local secondary index)

then you can query for a record with 'id' and 'city'.

update:

if you want to query for 'city'

KeyConditionExpression: "city = :city"

then you can just add global secondary index to the table.

Sign up to request clarification or add additional context in comments.

6 Comments

are you added "IndexName": "city-index" to your query?
Thank you. it works now. Is there also a way to specify multiple indices in the indexName property. E.g. it i want to query for city, street and housenumber?
wow. that's a bummer. This makes dynamodb practically useless for my purpose. (e.g. querying street addresses)
yes.. using dynamodb is not always recomended. but you can try to think on build composite key. if your streets has codes, so your 'city' key can be 'NY-2'. then you can query for city+street
@EyalCh let's suppose we have city and state in our database. id is the hash key and city as the range key. How to query using city and state conditions?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.