How to use “IN” statement in FilterExpression using array - dynamodb
amazon-dynamodb, aws-sdk, node.js
Solution
Please refer this answer
Summary:-
For fixed number of values in "IN" clause:-
var params = {
TableName : "Users",
FilterExpression : "username IN (:user1, :user2)",
ExpressionAttributeValues : {
":user1" : "john",
":user2" : "mike"
}
};
For more elements in array and forming the FilterExpression dynamically:-
var titleValues = ["The Big New Movie 2012", "The Big New Movie"];
var titleObject = {};
var index = 0;
titleValues.forEach(function(value) {
index++;
var titleKey = ":titlevalue"+index;
titleObject[titleKey.toString()] = value;
});
var params = {
TableName : "Movies",
FilterExpression : "title IN ("+Object.keys(titleObject).toString()+ ")",
ExpressionAttributeValues : titleObject
};
Problem
Checked AWS document but did not find any working example. Here is my attempt ``` var params = { TableName: "User", IndexName:"a-b-index", KeyConditionExpression: "Country = :country and #s = :status", FilterExpression: "Id IN (:e)", ExpressionAttributeValues: { ":country ": "USA", ":status": 1, ":e": "1" }, ExpressionAttributeNames: {"#s": "Status"} }; //get users dynamodb.query(params, function (err, data) { if (err) //error else { //success } }); ``` Got records but it is fetching record which have id `1` but i want to use array like this ``` var params = { TableName: "User", IndexName:"a-b-index", KeyConditionExpression: "Country = :country and #s = :status", FilterExpression: "Id IN (:e)", ExpressionAttributeValues: { ":country ": "USA", ":status": 1, ":e": ["1","2","3"] }, ExpressionAttributeNames: {"#s": "Status"} }; //get users dynamodb.query(params, function (err, data) { if (err) //error else { //success } }); ``` How can make above code as working.want to get records. syntax is correct and query run without error but i am not getting records