I am working on a project with Dynamodb and Nodejs. I need a solution for query on multiple fields like select * from table where name=this and age=22 and active=this and area=this. I need a solution with query only scan is not allowed to me. If it is possible then please let me know with a sample nodejs script.
-
1Please provide the key attributes for this table to provide the solution.notionquest– notionquest2017-03-31 08:09:01 +00:00Commented Mar 31, 2017 at 8:09
-
@notionquest you can use personId as key attribute.mandeepsinghn– mandeepsinghn2017-03-31 08:45:30 +00:00Commented Mar 31, 2017 at 8:45
Add a comment
|
1 Answer
Here is the code.
1) Change the table name
2) Change login credentials if you are using the AWS DynamoDB service. The below code uses local DynamoDB service
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
region : "us-west-2",
endpoint : "http://localhost:8000",
credentials : creds
});
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "tablename";
var params = {
TableName : table,
KeyConditionExpression : 'personId = :personIdval',
FilterExpression : '#name= :nameVal and age= :ageVal and active=:activeVal and area=:areaVal',
ExpressionAttributeNames : {
'#name' : 'name'
},
ExpressionAttributeValues : {
':personIdval' : '7',
':nameVal' : 'this',
':ageVal' : 22,
':activeVal' : 'this',
':areaVal' : 'this'
}
};
docClient.query(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});