Trying to select data with multiple parameters from DynamoDB

0

Hi, I have just started working on Dynamodb, i want to create a page on the basis of category, sub category and name using dynamodb and nodeJs However not able to do.

MYSQL Query: Select * from test Where category="1" and sub_category="2" and name="user";

however i don't know how to create this query on dynamodb, whenever I tried to define 2 primary key it return me an error so i have created local secondary key for same below is the table structure.

var params = {
TableName : "test",
KeySchema: [
{ AttributeName: "name", KeyType: "HASH"}, //Partition key
{ AttributeName: "sub_category", KeyType: "RANGE"} //Sort key
],
AttributeDefinitions: [
{ AttributeName: "name", AttributeType: "S" },
{ AttributeName: "sub_category", AttributeType: "S" },
{ AttributeName: "category", AttributeType: "S" },
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
},
LocalSecondaryIndexes: [ // optional (list of LocalSecondaryIndex)
{
IndexName: 'name_category',
KeySchema: [
{ // Required HASH type attribute - must match the table's HASH key attribute name
AttributeName: 'name',
KeyType: 'HASH',
},
{ // alternate RANGE key attribute for the secondary index
AttributeName: 'category',
KeyType: 'RANGE',
}
],
Projection: { // required
ProjectionType: 'ALL', // (ALL | KEYS_ONLY | INCLUDE)
},
},
// ... more local secondary indexes ...
],
};

DynamoDB Query:

Curriculum.findtest = function([category, sub_category, name], callback){
var params = {
TableName: "test",
IndexNames: "name_category",
KeyConditionExpression:"#name= :name and #sub_category = :sub_category and #category = :category",
ExpressionAttributeNames:{
"#category": "category",
"#sub_category ": "sub_category",
"#name": "name"
},
ExpressionAttributeValues: {
":category": category,
":sub_category": sub_category,
":name": name
}

        };  

 docClient.query(params, function(err, data) {    
        
    if (err) {  
        console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));    
    } else {  
        callback(null, data.Items);  
        return true;  
 }  
   
});  

}

However getting an error in console :

Unable to read item. Error JSON: {
"message": "Conditions can be of length 1 or 2 only",
"code": "ValidationException",
"time": "2019-01-18T06:17:04.470Z",
"requestId": "78406675-5485-4610-bb59-1ddc7976ab17",
"statusCode": 400,
"retryable": false,
"retryDelay": 32.41354621763527
}

I don't know what am i doing wrong. Kindly assist me.

Thanks in advance.

Edited by: DynamoDev on Jan 18, 2019 12:58 AM

asked 5 years ago3059 views
1 Answer
0

You can use KeyConditionExpression parameter to provide a specific value for the partition key. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression. ( scenario when you have both partition key as well as sort key)

Now, reason you are facing error 'Conditions can be of length 1 or 2 only' is because you are specifying 3 conditions within KeyConditionExpression. Please specify only partition key and sort key. In order to further refine your Query you can optionally provide a FilterExpression on other attributes. A FilterExpression determines which items within the results should be returned to you. All of the other results are discarded.

You can read more about same here: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

Lastly, you dont need to create a LSI for this purpose. You can perform same operation on original table

Hope this helps

AWS
answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions