Monday 5 October 2020

DynamoDB Nodejs - Query a column with array of strings

I have two columns genres and artists (both are arrays of strings) in a table in DynamoDB. A lambda function is written to get data from this table. The inputs to this lambda function are genres (array of strings - which is a subset of values in genres column) and artists (array of strings which is a subset of values in artists column).

Now i am trying to construct a query with FilterExpression that returns all columns that has at-least one of the values in genres column from genres input array or artists column from artists input array. Basically any match of atleast one string from both arrays in the respective columns.

This is what i have tried

 if (genres != null) {
            
            var genreArray = genres.split(",");

            var genresKeyObject = {};

            genreArray.forEach(function(value) {
                index++;
                var genreKey = ":genreValue" + index;
                expression_attributes[genreKey.toString()] = value;
                genresKeyObject[genreKey.toString()] = value;
            });

            filter_expression = filter_expression + " genres IN (" + Object.keys(genresKeyObject).toString() + ")";
        }


Here is an example of my FilterExpression and ExpressionAttributes values:

var params = {
            TableName: dynamodbTableName,
            IndexName: 'MonthYearIndex',
            KeyConditionExpression: "month_year = :month_year",
            FilterExpression: filter_expression,
            ExpressionAttributeValues: expression_attributes
        };

filter_expression : (artists IN (:artistValue1) OR genres IN (:genreValue2)) AND host_publisher_id = :host_pid
expression_attributes: {
    ":month_year": "2020-09",
    ":host_pid": "21",
    ":artistValue1": "Midwestern rockers",
    ":genreValue2": "Rock and Pop"
}


const data = await docClient.query(params).promise();

The above query is returning only 1 record, where as i can see that there are multiple records in db for this FilterExpression. I am trying to get all the records that match this query.

enter image description here



from DynamoDB Nodejs - Query a column with array of strings

No comments:

Post a Comment