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
A filter expression determines which items within the Query results should be returned to you. All of the other results are discarded. A filter expression is applied after a Query finishes, but before the results are returned.
A Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.
You can use the sort-key and apply the ScanIndexForward parameter in a query to sort in either ascending or descending order. Here I limit items returned to 1.
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 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With