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