17

I have a use case to apply begins_with on the primary sort key of an AWS Dynamodb table,
I am able to query the table using begins_with key condition from the AWS Console,
I wish to achieve the same using the AWS Javascript SDK.

enter image description here enter image description here

I have the following fields in my table -
1. user_id (Primary partition key)
2. user_relation (Primary sort key)

I tried the following code -

let AWS = require('aws-sdk');
let util = require('util');

AWS.config.update({
    region: 'us-east-1'
});

let connection = new AWS.DynamoDB.DocumentClient();

let params = {
    TableName: 'user_details',
    KeyConditionExpression: 'user_id = :user_id and user_relation begins_with :user_relation',
    ExpressionAttributeValues: {
        ':user_id': "1234",
        ':user_relation': "followed-by"
    }
};

console.log('getQuery Params => ', params);
let dynamoDb = util.promisify(connection.query).bind(connection);
let results = await dynamoDb(params);
console.log('results => ', results);

I am getting a syntax error, what is the correct way to use begins_with while querying an AWS DynamoDb table?

Error -

(node:40474) UnhandledPromiseRejectionWarning: ValidationException: Invalid KeyConditionExpression: Syntax error; token: "begins_with", near: "user_relation BE
GINS_WITH :user_relation"

References -
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property

DynamoDB,how to query with BEGINS_WITH

Can't Query DynamoDB with Begins_with for list of Items

3
  • Where are you getting the Syntax error, can you post the error as well? Commented Dec 5, 2019 at 10:30
  • @uday8486 added the error Commented Dec 5, 2019 at 10:32
  • 4
    user_id = :user_id and begins_with(user_relation, :user_relation) Commented Dec 5, 2019 at 10:55

1 Answer 1

36

Try :

const params = {
  TableName: 'user_details',
  KeyConditionExpression: '#user_id = :user_id and begins_with(#user_relation, :user_relation)',
  ExpressionAttributeNames:{
    "#user_id": "user_id",
    "#user_relation": 'user_relation'
  },
  ExpressionAttributeValues: {
    ":user_id": "1234",
    ":user_relation": "followed-by"
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

getting (node:40537) UnhandledPromiseRejectionWarning: ValidationException: Invalid KeyConditionExpression: Syntax error; token: "begins_with", near: "#user_relation b egins_with :user_relation"
Updated the answer, tested it. It works. begins_with is a function, called like begins_with(#user_relation, :user_relation)
Wondering why we need all these extra properties. All of that information fits inside KeyConditionExpress, can we not put it all there? ExpressionAttributeNames, and ExpressionAttributeValues are just name fluff. This could be so much simpler : (

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.