Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Query a Array of objects in DynamoDB using FilterExpression in scan operation

enter image description here

How to Query array of objects(workingDays) key containing only "Tue" in dynamoDb with Scan Operation,I have queried using filter Expression but i am getting no results.

var queryData = {
        TableName: tableName,
        FilterExpression: "contains (workingDays, :dayVal)",
        ExpressionAttributeValues: {
            ":dayVal": {
                S:"Tue"
            }
        }
    };

    console.log("getParams ==>", queryData)
    dynamodb.scan(queryData, function (err, details) {
        if (err) {
            console.log(err, err.stack); // an error occurred
            callback(err, null)
        }
        else{
           callback(null, details)

        }
    })
like image 529
RAHUL SRV Avatar asked Jan 02 '19 07:01

RAHUL SRV


Video Answer


1 Answers

ExpressionAttributeValues in your query contains String ["S"] as 'key' for value 'Tue', where as in your table, 'workingDays' is a list of map object containing value for day keys.

Try below code:

var queryData = {
    TableName: tableName,
    ExpressionAttributeNames: {
         "#workingDays": "workingDays",
    },
    FilterExpression: "contains (#workingDays, :dayVal)",
    ExpressionAttributeValues: {
      ":dayVal": {
          "day":"Tue"
      }
   }
};

console.log("getParams ==>", queryData)

docClient.scan(queryData, function (err, details) {
    if (err) {
        console.log(err, err.stack); // an error occurred
       // callback(err, null)
    }
    else{
       // callback(null, details)
       console.log(details);
   }
});
like image 96
Amrit Pal Singh Avatar answered Nov 10 '22 00:11

Amrit Pal Singh