skip to Main Content

As the title said, I can’t query for all sort key in primary key in DynamoDB. It just got difference error whatever I tried some difference approach.

Also, my sort keys don’t start with the same string, so BEGIN_WITH is unusable.

I have already tried some difference command, like

const command = new QueryCommand({
    TableName: "MyTable",
    KeyConditionExpression: 'PK = :pk,
    ExpressionAttributeValues: {
        ":pk": tempPK,
    },
});

It got this error "One or more parameter values were invalid: Condition parameter type does not match schema type". It seems like maybe the ‘KeyConditionExpression’ require both primary and sort key.

I have also tried using "attribute_exists"

const command = new QueryCommand({
    TableName: "MyTable",
    KeyConditionExpression: 'PK = :partitionKey AND attribute_exists(SK)',

    ExpressionAttributeValues: {
        ':partitionKey': tempPK,
    },

});

It got this error "Invalid operator used in KeyConditionExpression: attribute_exists". As it said, attribute_exists doesn’t vaild.

So, I came up with this. By using greater that sign, it should work.

const command = new QueryCommand({
    TableName: 'MyTable',
    KeyConditionExpression: 'PK = :partitionKey AND SK > :minSortKey',
    ExpressionAttributeValues: {
        ':partitionKey': tempPK,
        ':minSortKey': '',
    },

});

Yet it still error, got "Query key condition not supported."

I began just two weeks ago, and I would greatly appreciate any assistance or guidance. Thank you!

2

Answers


  1. My assumption here is that you are passing the wrong format to your client. You never show how you create the client or the value of tempPK.

    I wrote a blog on the different clients which you can use and the parameters you should pass, being unfamiliar with DynamoDB I recommend you use the high level client. Take a read of the blog, it will most likely help you understand your issue.

    const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
    const { DynamoDBDocumentClient, QueryCommand } = require("@aws-sdk/lib-dynamodb");
    
    const client = new DynamoDBClient(});
    const docClient = DynamoDBDocumentClient.from(client);
    
    // Notice its a simple string value, as high level client accepts native JSON
    const tempPK = "user123";
    
    const command = new QueryCommand({
        TableName: "MyTable",
        KeyConditionExpression: 'PK = :partitionKey',
        ExpressionAttributeValues: {
            ':partitionKey': tempPK,
        },
    
    });
    
    docClient.send(command)
      .then((data) => {
        console.log(data.Item);
      })
      .catch((err) => {
        console.error(err);
      });
    

    This code will return all of the items which match tempPK partition key.

    Login or Signup to reply.
  2. Your first example appears like the correct approach. I have a similar call where I’m getting all records using only the primary key. When I compare my code with yours, I do see a couple of differences though.

    const command = new QueryCommand({
        TableName: "MyTable",
        KeyConditionExpression: 'PK = :pk,    <--MISSING END QUOTE
        ExpressionAttributeValues: {
            ":pk": tempPK,     <--I HAVE DIFFERENT SYNTAX
        },
    });
    

    Here is an updated version based on the query I have in my code. Hopefully this helps.

    const command = new QueryCommand({
        "TableName" : "MyTable",
        "PrimaryKey": "PK",
        "KeyConditionExpression": "PK = :pk",
        "ExpressionAttributeValues": {
            ":pk": {
                "S": tempPK
            }
        }
    });
    

    I’m not sure if the value needs to be in quotes or not. It does for me. So try both of these. "S" here assumes your primary key is a string. If it’s a number, replace with "N".

    "S": tempPK and "S": "tempPK"

    One final tip. DynamoDB can be particular about double/single quotes. Especially when specifying indexes in the query. Keep that in mind when building your queries.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search