Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use “IN” statement in FilterExpression using array - dynamodb

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

like image 759
Nirmal Goswami Avatar asked Oct 27 '16 11:10

Nirmal Goswami


People also ask

What is Filterexpression in DynamoDB?

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.

Does DynamoDB scan return all items?

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.

How do I sort DynamoDB Query results?

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.


1 Answers

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 }; 
like image 65
notionquest Avatar answered Sep 18 '22 08:09

notionquest